HelpWhat's wrong with my code?

Posts 1–15 of 15 · Page 1 of 1
What's wrong with my code?
Code:
#include <iostream>
#include <string>
using namespace std;

int main ()
{
	string makeCar;
	string modelCar;
	int numCars;
	int gasMileage;
	int sumGasMileage = 0;
	int avgGasMileage;
	int i;
	int min = 0;
	
	
	while((makeCar) && (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);
		
		cout << "Please enter the mpg for each car: ";
		for(i=0; i < numCars; i++)
			{
				cin >> gasMileage;
				sumGasMileage += gasMileage;
				avgGasMileage = sumGasMileage/numCars;
			
			}
			
		cout << "\n";
		cout << makeCar << " " << modelCar << " average mpgh = " << avgGasMileage;
		cout << "\n";
		
		if(i==0)
		{
			min = avgGasMileage;
		}
		
		if(min<avgGasMileage)
		{
			min = avgGasMileage;
		}
		cout << "worst mpg average = " << min;
	}
	
	return 0;
}
When I try to compile I get this error: no match for 'operator!=' in 'modelCar != 1685024357'
and I'm not sure why. :\
Heey, I came up with a quick solution. I hope it works for you

Code:
#include <iostream>
#include <string>
#include "conio.h"
using namespace std;

int main ()
{
	string makeCar;
	string modelCar;
	int continueProg = 2;
	int numCars;
	int gasMileage;
	int sumGasMileage = 0;
	int avgGasMileage;
	int i;
	int min = 0;
	
	
	while(continueProg != 3)
	{
		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);
		
		cout << "Please enter the mpg for each car: ";
		for(i=0; i < numCars; i++)
			{
				cin >> gasMileage;
				sumGasMileage += gasMileage;
				avgGasMileage = sumGasMileage/numCars;
			
			}
			
		cout << "\n";
		cout << makeCar << " " << modelCar << " average mpgh = " << avgGasMileage;
		cout << "\n";
		
		if(i==0)
		{
			min = avgGasMileage;
		}
		
		if(min<avgGasMileage)
		{
			min = avgGasMileage;
		}
		cout << "worst mpg average = " << min;
		continueProg++;
		_getch();
	}
}
There's the code, I hope it works
I'm sure you'll understand the code..
message back if you need some help
Bai bai
Quote Originally Posted by darkunit22 View Post
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.
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);
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);
Wow I can't believe I didn't see that semi-colon there -,-....
But I ended up doing this:
Code:
while(!(makeCar == "done") || (modelCar == "done"))
Which it did get rid of the error but if when i get to the part where it asks to enter the make and model of the car and I type done done it'll still continue through the loop.
while(!(makeCar == "done") || (modelCar == "done"))


while(!((makeCar == "done") || (modelCar == "done")))

: )

The way you had it would be read as...

!(makeCar == "done") = expression 1

||

(modelCar == "done") = expression 2

which isn't what you want. You want to finish when either is 'done', right?
Oh yah I had the extra () just forgot to type it since I made that last post from mobile. :\

But see what's weird is that I can type done done when prompted but it'll go through all the way and then stop. I want it to stop right after entering done done.
Because you're not asking for the input until inside the loop....so the loop has to run before it could get "done" typed in. You need to change the logic a little bit.
Do you think it would make sense to do an if statement right after the make and model are prompted saying if make and model = done then break?
Yes, it's missing a certain elegance, but that's how I did it to change the least amount of code.

Code:
while(true)
	{
		cout << "Please enter the make and model of the car (e.g, Toyota Prius): ";
		cin >> makeCar;
		cin >> modelCar;
		if((makeCar == "done") || (modelCar == "done"))
		{
		break; // leave the main menu while-loop
		}
..it works. But imo, not pretty.

Maybe look up "Console menu system in C++" to give you a few ideas.
Yah that is what I ended up doing. And I would but I'm supposed to code this specific program with certain specifications that were predefined...

And could I in contact with you in another way?
Ah I see. Yeah, the only instant-messaging program I use is Skype. My username is the same as here on mpgh.
Thank you abuckau907 for clearing it all up for me! Everything is running nicely and error free

/Closed
You can't compare a std::string with an int.
^ This and why is noone telling him to use
Code:
modelCar.compare("done") == 0
???
OP's original compiler error was
... I get this error: no match for 'operator!=' in 'modelCar != 1685024357'
^^comparing string and int

He was incorrectly trying to compare 2 strings.
Posts 1–15 of 15 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us