How to check if a string is "0" ?
Jun 26, 2013 at 8:07pm
HI!
when i write :
1 2 3 4 5
|
string tmp ;
cin >> tmp;
if (tmp== "0") cout << "Zero" ;
//or instead of upper line :
if (!tmp.compare ("0")) cout << "Zero" ;
|
I see an error which says string couldn't be compared with int ;
But I thought if I put that in this : " "
It will count it as string .
Thanks in advanced
Jun 26, 2013 at 8:18pm
string tmp ;
cin >> tmp;
if (tmp== "0") cout << "Zero" ; |
There is nothing wrong with this code.
Are you sure this is the actual code that is giving you the error?
Are you sure you're not doing this?
1 2 3
|
if(tmp == 0) // <- this would be wrong because 0 is not a string
if(tmp == "0") // <- this is fine
|
Topic archived. No new replies allowed.