2

I have a while loop that asks the user's query and takes in a response, could be a word, could be a sentence. The output is boggling me.

std::string response;

while (response != "!quit")
    {
        cout << "Query? ";
        cin >> response;
        if (response == "!quit")
        {
            cout << "Thank you for using this program!";
            exit(0);
        }
    }

The output for this looks like this:

Query? how are you
Query? Query? Query? how are you today
Query? Query? Query? Query? hi
Query?

why is it printing out "query" for the same number of words as the response?

JaMiT
  • 14,422
  • 4
  • 15
  • 31
Alexander
  • 127
  • 1
  • 10
  • 1
    https://stackoverflow.com/questions/30005015/whats-the-difference-between-getline-and-stdistreamoperator – rafix07 Oct 30 '20 at 20:43
  • How is `response` defined? – DarkAtom Oct 30 '20 at 20:44
  • 1
    @DarkAtom as a string – Alexander Oct 30 '20 at 20:44
  • @Alexander You mean a `std::string`? – πάντα ῥεῖ Oct 30 '20 at 20:54
  • Does this answer your question: [C++ “cin” only reads the first word](https://stackoverflow.com/questions/9469264/c-cin-only-reads-the-first-word), or this: [std::cin.getline( ) vs. std::cin](https://stackoverflow.com/questions/4745858/stdcin-getline-vs-stdcin) – πάντα ῥεῖ Oct 30 '20 at 20:58
  • 1
    **Debugging tip:** Your loop is controlled by the `response` variable. When the loop seems to be misbehaving, take a look at the value of that variable (e.g. `cout << "response is '" << response <<"'.\n";` right after you read it from `cin`). – JaMiT Oct 30 '20 at 21:33

2 Answers2

5

std::cin is going to read until it meats a whitespace.

So when you input "how are you ", cin is going to be called thrice, so that it consumes all three whitespaces. So the first cin will read "how ", the second "are ", and the third "you ".

So what teaches you is that in order to read a sentence, you need another approach, e.g. by using std::getline, like this:

std::getline(std::cin, response)

Read more in What's the difference between getline and std::istream::operator>>()?


PS: Note the trailing newline character that might appear after reading a number with cin, which you might want to consume, e.g. by using getchar();.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
3

It could be a word, could be a sentence.

When you want to read a word, you can use cin >> response;. This will stop reading at the next whitespace character found.

When you want to read a sentence (an entire line), you need to use getline(cin, response);. This will stop reading at the next new-line \n character found.

Therefore, since you are expecting one input on a single line, use the second function.

EDIT: Also, please note! When using getline() after a non-string I/O operation (like cin >> n;, where n is an int variable), you need to know that the non-string I/O functions don't "eat" the \n character. getline() will find it, consume it and stop, so it is best to eat it yourself with an empty cin.getline(); call.

DarkAtom
  • 2,589
  • 1
  • 11
  • 27