1

Goal


New to c++ and haven't found a definitive answer on this question anywhere else. I'm working on a simple program that reads in a message typed in from the user on from within the console. It's an exercise for using string variables/concatenation.

I need to create loop that reads in the user input, which may contain multiple lines of input from the command shell.

So my function needs to read that input, while ignoring newlines, and end if a user inputs two "&&" on a new line.

Attempt


So here's my function:

string get_message() {
    string message, line;
    cout << "Enter the message > ";
    cin.ignore(256, '\n');
    while (getline(cin, line) && line != "&&") {
        message = message + " " + line;
        cout << message;
        cin.clear();
    }
    return message;
}

The issue I'm running into is that within the while loop, until && is found, the loop content doesn't appear to run. Meaning when I cout << message I only get the single previous line of input.

Sample Run


Enter the Message >  Messages.. this is a new message.
I'm a message on a new line, look at me.
New line.
&&
"New line." <--- from console cout

Result: New line.

Questions:


  • When does the loop content get called?
  • Why do I only get the previous line and not ALL previously (supposedly) concatenated lines?
  • Is there a better way to code this?
10
  • "the loop content doesn't appear to run" – it certainly does. where are you printing message? Commented Sep 7, 2015 at 18:40
  • Within the loop (edited) Commented Sep 7, 2015 at 18:40
  • You realize that cin.ignore(256, '\n'); is eating your first input line, right? Commented Sep 7, 2015 at 18:44
  • What do you think this does cin.ignore(256, '\n');? Mind you I can't work out why two lines appear to be ignored. Commented Sep 7, 2015 at 18:44
  • The correct way to ignore a line of input (if that's what you want to do) is cin.ignore(numeric_limits<streamsize>::max(), '\n'); Commented Sep 7, 2015 at 18:46

1 Answer 1

2

Breaking this down:

string get_message() {
    string message, line;
    cout << "Enter the message > ";

Standard stuff. Nothing to see here. Move along.

    cin.ignore(256, '\n');

Discard the first line or 256 characters, whichever comes first. Probably not what you want to do. Ideologically, if you think there might be crap in already the stream, empty the stream before calling the function. Anyway, definitely part of OP's problem.

    while (getline(cin, line) && line != "&&") {

While successfully got a line AND line is not "&&". Looks good. NOte: The new lines are stripped by the getline function because they're the token delimiter and leaving them in the returned token or leaving them in the stream would just cause problems.

        message = message + " " + line;

Append line to message

        cout << message;

Write message to output. There is no flushing going on, so when message makes it to the screen is unpredictable. This may be a cause of part of OP's problem.

        cin.clear();

Clear error condition on cin. Not needed. If cin was in an error condition, the while loop would not have entered.

    }
    return message;
}

Normal stuff. Nothing to see here, but if the program ends shortly after this point, cout.flush() is called, std::flush is sent to cout, or there is a cout << endl;, cout will be flushed and the messages will suddenly appear.

So, using OP's input:

Messages.. this is a new message.

This is obliterated by cin.ignore

I'm a message on a new line, look at me.

Should appear eventually. No clue why OP doesn't see it. I'm unable to reproduce.

New line.

Should appear eventually.

&&

Ends input.

Output should be:

I'm a message on a new line, look at me. I'm a message on a new line, look at me. New line.

And I'm stumped as to why the OP doesn't get this the first time cout gets flushed. As I said, unable to reproduce.

Return should be:

 I'm a message on a new line, look at me. New line.
Sign up to request clarification or add additional context in comments.

1 Comment

I really appreciate the answer! Definitely helps clear things up

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.