I have already had this problem but i forgot the reason for it's appearance | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

9th Sep 2018, 8:34 AM
Oleg Storm
Oleg Storm - avatar
4 Answers
+ 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.
9th Sep 2018, 8:57 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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')
9th Sep 2018, 8:58 AM
Dew27
Dew27 - avatar
+ 1
string::at returns a char. Use single quote 'a' for char literal, double quote is for string literal.
9th Sep 2018, 8:56 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
+ 1
thanks guys, you are the best😁
9th Sep 2018, 9:00 AM
Oleg Storm
Oleg Storm - avatar