0
I have already had this problem but i forgot the reason for it's appearance
for(int i = 0; i < word.length(); ++i){ std::string letter = word.at(i); if(letter == "a") //error And here is another ex: for(int i = 0; i < word.length(); ++i){ std::string letter = word.at(i); if(word.at(i) == "a") //also error and i know why but not //completely to get rid of this
4 Antworten
+ 4
You are comparing a string to a character. You were also trying to assign a character to a string variable. Take note of the difference between double quotes and single quotes. The at() function in string class returns a character.
+ 2
/* at() function return character at the position i. Hence, data type for letter should be char instead of std::string*/
char letter = word.at(i);
/* A character (char value) is enclosed between a single quote('a') rather than double quote ("a"). */
if(letter == 'a')
+ 1
string::at returns a char. Use single quote 'a' for char literal, double quote is for string literal.
+ 1
thanks guys, you are the best😁