while loop does not execute all contained code

Here is my code:

#include <iostream>

using namespace std;

int main()
string message;
char tF = 'n';

while (tF == 'n'){
cout << "Please enter message to encode: " << endl;
getline(cin, message);

cout << " Is this correct? y/n" << endl << message << endl;
cin >> tF;

return 0;

It works correctly the first time, but if you input n for the tF(true/false) variable, then it skips: getline(cin, message);, << message << endl; on the next loop.

Why is this?
Last edited on
the issue is mixing getline and cin statements. they leave the stream in different incompatible states. clear the buffer to avoid this, google to see how (its really late here, getting sleepy).

and remember this one; its easy to forget but it comes up a lot.
Last edited on
Hello dallin56,

There is nothing wrong with your code, but with your understanding of formatted and unformatted input, i.e., (std::getline(), unformatted input, and cin >> tf, formatted input.

Formatted input will extract from the input buffer up to a white space or newline, (\n), whichever comes first.But it leaves the (\n) in the input buffer.

Unformatted input, the "std::getline", will extract everything from the input buffer including the (\n) and then disgard the (\n) leaving the input buffer empty.

As jonnin mentioned there are several ways to clear the input buffer:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
std::cin.ignore(1000, '\n').
Or use the defaults of the function std::cin.ignore();  // <--- 1 character or the "\n" whichever comes first. 

For your code:
#include <iostream>
#include <string>

//using namespace std;  // <--- Best not to use.

int main()
    std::string message;
    char tF = 'n';

    while (tF == 'n')
        std::cout << "\n Please enter message to encode:\n> ";
        std::getline(cin, message);

        //std::cout << "\n Is this correct? y/n: " << endl << message << endl;
        std::cout << " Is this correct? y/n: ";
        std::cin >> tF;

        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

    return 0;

This is untested, but should work.

First your program defines a "std::string" then you use "std::getline" and finally a "cout" statement the prints the "std::string" message. All of these need the header file "<string>" to work properly otherwise your compiler should be complaining.

Other than the addition of line 2 line 20 is the most portable way of writing this that can be used by any compiler and header files.

In line 13 I changed the prompt a little.

For line 17 There is no real point in printing the message that was just entered. You can still see what was entered you just need to know if it is correct or not.

Some of the changes are a suggestion, so see what you think.

Hello dallin56,

Should have said this earlier:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.


Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

After having chance to test the program I realized I did not add the line #include <limits> and missed qualifying the "cin" in the "std::getline" as "std::cin".

After that if was minor things to get the output to look like this:

 Please enter message to encode:
 > test

 Is this correct? y/n: n

 Please enter message to encode:
 > test 2

 Is this correct? y/n: y

 Your message is: test 2

The last line is this: std::cout << "\n\n Your message is: " << message << '\n'; following the while loop.

Prefer using the new line (\n) over "endl" as much as possible. "endl" is a function that takes time to process. The more that you have the more you may notice your program running slower.

Registered users can post here. Sign in or register to post.