Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   need help cant evaluate a string??? (http://www.go4expert.com/forums/help-evaluate-string-t20811/)

Brett.h 30Jan2010 01:58

need help cant evaluate a string???
 
I am writing a program that will evalute a string and check for spaces, when I try to compile it I get this error message:

Error 1 error C2446: '==' : no conversion from 'const char *' to 'int' c:\documents and settings\brett\my documents\visual studio 2008\projects\project1\ai learn algorithm\ai learn algorithm\test.cpp 12

what is wrong, and how can I fix it

all help is appreciated,

Brett


getline(cin, teststr);
int counter = 0;
cout << teststr[counter];
if (teststr[counter] == " "){
cout << "test complete";
}

Gene Poole 30Jan2010 23:26

Re: need help cant evaluate a string???
 
Use single quotes for a single character. Double quotes designate an array of characters that decays to a pointer to type char when used as above. Try this:

Code:

  if (teststr[counter] == ' '){

murugaperumal 26Feb2010 11:04

Re: need help cant evaluate a string???
 
Dear Friend,

To compare the two string ,instead of "==" operator use the compare function.

Sample code
Code:

string str1 ("green apple");
if (str1.compare(6,5,"apple") == 0)
cout << "still, " << str1 << " is an apple\n";



All times are GMT +5.5. The time now is 20:46.