I can not understand what is wrong, why does the comparison in the while not work?

 #include <iostream> int main() { using namespace std; char word[20]; int count = 0; cout << "<Enter separate letter 'q' in the end of text>\n"; cout << "Enter the text:\n"; cin >> word; while (word != "q") { count++; cin >> word; } cout << count << " words in the text.\n"; system("pause"); return 0; } 

Input example: one two three q

There should be a conclusion: 3 words in the text.

    1 answer 1

    Write for example

     while (strcmp(word,"q")) 

    (comparison of lines in style C); or

     while (word != string("q")) 

    Or make the word not char[] , but string — to use the == operator for string .

    Otherwise, you compare whether the string literal "q" and the word array are at the same address . It is clear that in different ...