Comparing integer with string

Hi guys, I am troubled in understanding why I cannot compare an int with a string and can you please tell me what should I use instead? Thank for help



#include <iostream>
#include <cmath>
using namespace std;

int main(){

int age;
char mood;
char orientation;

cout<<"Insert Age"<<endl;
cin>>age;
cout<<"Insert Sex"<<endl;
cin>>mood;

if (age>18 && mood == "sad")
{
cout<<"buhahah"<<endl;

}


system ("pause");
}
}


system ("pause");
}
Last edited on
closed account (2LzbRXSz)
You have to turn the string into an int.

You can do this using stringstream, or atoi :)
http://www.cplusplus.com/reference/sstream/stringstream/stringstream/
http://www.cplusplus.com/reference/cstdlib/atoi/

By the way, you should probably put your code between code tags [ code] [/ code] (but without the spaces) that way others can easily look at your code and refer to lines that need changing.

You also have two system pauses.
1
2
3
4
5
6
7
8
9
char mood;
char orientation;

cout<<"Insert Age"<<endl;
cin>>age;
cout<<"Insert Sex"<<endl;
cin>>mood;

if (age>18 && mood == "sad")


mood is a char not a string. Either change it to a std::string and compare with == or to a char array and compare with strcmp ( http://www.cplusplus.com/reference/cstring/strcmp/ )
Topic archived. No new replies allowed.