Basic String input

33,292

You see, when you enter "John Smith" as a input first cin >> name will not read the entire line, but the the contents of the line until the first space.

So, after the first cin, name variable will contain John. There will still be Smith\n in the buffer, and you've solved this using:

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

Note: As Konrad Rudolph suggested, you really shouldn't use 256 or any other magic numbers in your code. Rather use std::numeric_limits<std::streamsize>::max(). Here is what docs says about the first argument to istream::ignore:

Maximum number of characters to extract (and ignore). If this is exactly numeric_limits<streamsize>::max(), there is no limit: As many characters are extracted as needed until delim (or the end-of-file) is found.

cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n') ;

My question is, why is there no newline coming after the "Please enter your full name again: "?

Because you're not outputing one to the stdout, and the user didn't get chance to press Enter. getline will read Smith\n from the buffer, and it will continue immediately. It will not echo any newline characters to your console - getline doesn't do that.

Suddenly I get a newline after you enter your full name again. It's not really a huge issue to be honest. But I wouldn't mind knowing what happened if anyone can help me. Thanks!

It is the newline that user enters with the Enter key, it is not coming from your program.

Edit Pressing Enter in terminal usually (depending on the terminal setup) does few separate things:

  1. Inserting \n into the input buffer
  2. Flushing the input buffer
  3. Shifting the input cursor one line down
  4. Moving the input cursor to the beginning of the line
Share:
33,292
user2779581
Author by

user2779581

Updated on August 02, 2022

Comments

  • user2779581
    user2779581 almost 2 years

    I've just came across this bit of code that allows users to input strings in the command prompt. I'm aware of what they do and it's all great. But I have a question in regards to the cin and getline() functions.

    string name ;
    cout << "Please enter your full name: " ;
    cin >> name ;
    cout << "Welcome " << name << endl ;
    cout << "Please enter your full name again please: " ;
    getline(cin , name) ;
    cout << "That's better, thanks " << name << endl ;
    return 0 ;
    

    Now when this is output, I get something along the lines of: (using john smith as the input)

    Please enter your full name: john smith
    Welcome John
    Please enter your full name again: That's better thanks Smith
    

    I understand why this happens, the getline is still reading from the input buffer and I know how to fix it. My question is, why is there no newline coming after the "Please enter your full name again: "? When I alter the code to:

    string name ;
    cout << "Please enter your full name: " ;
    cin >> name ;
    cout << "Welcome " << name << endl ;
    cout << "Please enter your full name again please: " ;
    cin.ignore( 256, '\n') ;
    getline(cin , name) ;
    cout << "That's better, thanks " << name << endl ;
    return 0 ;
    

    Suddenly I get a newline after you enter your full name again. It's not really a huge issue to be honest. But I wouldn't mind knowing what happened if anyone can help me. Thanks!

  • Konrad Rudolph
    Konrad Rudolph over 10 years
    Please don’t use magic numbers in code. Correct here would be std::numeric_limits<std::streamsize>::max().
  • user2779581
    user2779581 over 10 years
    Thanks for the tip regarding the cin.ignore function, I'll be sure to note that down for further use. So would it be correct to say that the return key does two separate things when pressed in the command prompt, both entering /n as a part of the input and shifting the input stream down by a line? (I know this terminology isn't correct but it's the only way I can put it across) Otherwise wouldn't the 'Smith/n' left in the buffer act as if the user had pressed Return again? Apologies for this comment not being formatted at all, I'm new to this, thanks for your replies though!
  • user2779581
    user2779581 over 10 years
    Thanks a lot! I'm sure this hardly even counts as a real problem but it's great that you took the time to answer it. It's much appreciated, thank you.
  • user2779581
    user2779581 over 10 years
    Ok if anyone is still listening, I've attempted to use the std::numeric_limits<std::streamsize>::max() solution in my cin.ignore function, and a whole bunch of stuff is coming up in the command prompt saying that this basically just won't work. Are there certain libraries that I need to declare to be able to use this? Currently I just have #include <string> #include <iostream> using namespace std ; Any help would be greatly appreciated, thanks guys.
  • Nemanja Boric
    Nemanja Boric over 10 years
    @user2779581 #include <limits>
  • user2779581
    user2779581 over 10 years
    I know the comments section says not to include thanks, but I'd feel rude without expressing gratitude, so thanks.
  • Nemanja Boric
    Nemanja Boric over 10 years
    @user2779581 Or you could just accept this answer if it is working for you :). Jk, you're welcome!
  • user2779581
    user2779581 over 10 years
    Woops, I didn't see the answer resolved button :P Thanks again guys!