
Originally Posted by
darkunit22
Code:
int main ()
{
while((makeCar) && (modelCar) != 'done');
{
cout << "Please enter the make and model of the car (e.g, Toyota Prius): ";
cin >> makeCar;
cin >> modelCar;
...
}
When I try to compile I get this error: no match for 'operator!=' in 'modelCar != 1685024357'
and I'm not sure why. :\
I pasted the code into an empty project and tested -->
Very first thing I notice (after I get the string errors to go away) is that the outputs never get reached.....cout is never called.
Set a breakpoint on the while loop, and the first cout --> the while keeps getting called, but it never drops down to the body of the while..bizarre to say the least! Then I noticed...
There is a
semi-colon after the while, and there shouldn't be. !!! Once your code actually compiles, it will run into an infinite loop.
The first problem was how you're comparing the string...what you mean to put is while make != "done" && model != "done"
Code:
while((makeCar) != "done" && (modelCar) != "done")
{
cout << "Please enter the make and model of the car (e.g, Toyota Prius): ";
cin >> makeCar;
cin >> modelCar;
do
{
cout << "Please enter the number of " << makeCar << " " << modelCar << "'s that we own (1-10): ";
cin >> numCars;
}while(numCars < 1 || numCars > 10);
I'm not sure how to make the string compare prettier...there must be another syntax. ?
while((makeCar) && (modelCar) != 'done');
I think that would be interpreted as
1. makeCar == true or false, if true then go #2
2. check modelCard != 'done'
which isn't what you want. Step 1 is "Check if the string is true" ...., very C-like.
OR
makeCar and modelCar addresses' are ANDed together, which is where you're getting that number, then comparing that to the string.
edit: you should change your control structure, like the post above --> only use 1 variable to keep the outer while loop going, it will simplify it a little.
and use parenthesis ( )'s to group statements so everything is more explicit...instead of
5 + 2*9
5 + (2*9)
so we know to multiply first, then add. Which we all do, but the order-of-operations for C++ are a little harder. The while would go from
while((makeCar) && (modelCar) != 'done') // This makes sense in english, but not to compiler. Which variable is compared to 'done' ??
to
while((makeCar != "done") && (modelCar != "done"))
Imo. Maybe someone else will comment. Anyway, it's all about the order of operations. And I'm not sure about the difference between using single vs. double quote for your strings. I thought single chars use a single-quote, and arrays of chars have to use double-quote. I used "done" to get it to compile.
tldr, but hope it helped something.
*
my orig. from above. ..looks funny to me. Why would a variable name be surrounded by ()'s.
while((makeCar) != "done" && (modelCar) != "done")
{
cout << "Please enter the make and model of the car (e.g, Toyota Prius): ";
cin >> makeCar;
cin >> modelCar;
do
{
cout << "Please enter the number of " << makeCar << " " << modelCar << "'s that we own (1-10): ";
cin >> numCars;
}while(numCars < 1 || numCars > 10);
slightly re-arranged parens ..this is style I prefer.
Code:
while((makeCar != "done") && (modelCar != "done"))
{
cout << "Please enter the make and model of the car (e.g, Toyota Prius): ";
cin >> makeCar;
cin >> modelCar;
do
{
cout << "Please enter the number of " << makeCar << " " << modelCar << "'s that we own (1-10): ";
cin >> numCars;
}while(numCars < 1 || numCars > 10);
or even ..but then you might have to think about order of operations.
Code:
while(makeCar != "done" && modelCar != "done")
{
cout << "Please enter the make and model of the car (e.g, Toyota Prius): ";
cin >> makeCar;
cin >> modelCar;
do
{
cout << "Please enter the number of " << makeCar << " " << modelCar << "'s that we own (1-10): ";
cin >> numCars;
}while(numCars < 1 || numCars > 10);