7 Replies - 354 Views - Last Post: 25 September 2013 - 06:35 PM Rate Topic: -----

#1 DARKUNIT22  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 25-September 13

no match for 'operator!=' Error

Posted 25 September 2013 - 02:43 PM

Here is my 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 mpg = " << avgGasMileage;
		cout << "\n";
		
		if(i==0)
		{
			min = avgGasMileage;
		}
		
		if(min<avgGasMileage)
		{
			min = avgGasMileage;
		}
		cout << "worst mpg average = " << min;
	} 
	
	return 0;
}


When I try to build it, I get"no match for 'operator!=' in 'modelCar != 1685024357'" error and I'm not exactly sure why. I am a a medium beginner.

Is This A Good Question/Topic? 0
  • +

Replies To: no match for 'operator!=' Error

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: no match for 'operator!=' Error

Posted 25 September 2013 - 02:53 PM

There are a couple of things wrong with the following:

while((makeCar) || (modelCar) != 'done');


First single quotes signify a single character not a string. Second you need comparisons for both your make and model.

while((makeCar != someValue) || (modelCar != someValue));


Replace someValue with the proper string.

And be careful using the OR and the negation operators, I doubt that logic will produce your desired results. I normally recommend using "positive" logic whenever possible with a single negation after if required.

while(!((makeCar == someValue) || (modelCar == someValue)));



Jim
Was This Post Helpful? 0
  • +
  • -

#3 DARKUNIT22  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 25-September 13

Re: no match for 'operator!=' Error

Posted 25 September 2013 - 03:12 PM

View Postjimblumberg, on 25 September 2013 - 02:53 PM, said:

There are a couple of things wrong with the following:

while((makeCar) || (modelCar) != 'done');


First single quotes signify a single character not a string. Second you need comparisons for both your make and model.

while((makeCar != someValue) || (modelCar != someValue));


Replace someValue with the proper string.

And be careful using the OR and the negation operators, I doubt that logic will produce your desired results. I normally recommend using "positive" logic whenever possible with a single negation after if required.

while(!((makeCar == someValue) || (modelCar == someValue)));



Jim

Ah so strings don't get single qutations. Didn't know that, thank you! Let me test this out and see how it goes.
Was This Post Helpful? 0
  • +
  • -

#4 DARKUNIT22  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 25-September 13

Re: no match for 'operator!=' Error

Posted 25 September 2013 - 05:21 PM

Hmm it still seems to not be working.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: no match for 'operator!=' Error

Posted 25 September 2013 - 05:39 PM

Hmm, that doesn't seem like much of a description of a problem.

Post your modified code, the complete error messages exactly as they appear in your development environment. Then ask specific questions about the code and provide any other relevant information.


Jim
Was This Post Helpful? 1
  • +
  • -

#6 DARKUNIT22  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 25-September 13

Re: no match for 'operator!=' Error

Posted 25 September 2013 - 05:50 PM

Oops I'm sorry.

But I did as you advised:
while(!(makeCar == "done") || (modelCar == "done"))


And also got rid of that semi-colon at the end.
It compiles with no errors or warnings and runs but when I type 'done done' when it prompts me for the make of the car and the model of the car it does not stop the loop.
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: no match for 'operator!=' Error

Posted 25 September 2013 - 06:28 PM

Hi, if you want the loop to stop after you've entered the make and model, you could use an if statement with a break statement, or an if statement that excludes the rest of the code within the loop.
Was This Post Helpful? 0
  • +
  • -

#8 DARKUNIT22  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 25-September 13

Re: no match for 'operator!=' Error

Posted 25 September 2013 - 06:35 PM

Thanks for the reply! :D And I actually figured it out on my own and everything works now. Thanks Jim and #define.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1