Using getline(cin, s) after cin [duplicate]
Asked Answered
M

13

49

I need the following program to take the entire line of user input and put it into string names:

cout << "Enter the number: ";
int number;
cin >> number;

cout << "Enter names: ";
string names;

getline(cin, names);

With the cin >> number command before the getline() command however (which I'm guessing is the issue), it won't allow me to input names. Why?

I heard something about a cin.clear() command, but I have no idea how this works or why this is even necessary.

Musetta answered 21/4, 2011 at 5:29 Comment(2)
Assuming you typed: 5<enter>John<enter>. Then cin >> number reads JUST 5. leaving the new-line (enter) character on the stream. Thus when you try and read the name with getline(cin,name) it reads to the end of line. BUT NOTE there is a new-line character just there ready to be read (thus names will be empty (because you did not read off the new-line character after the 5). If you want to switch between >> and getline() you need to be carefull about trailing end of lines on your input.Emu
@LokiAstari: That's a better answer than any of those posted below. Could you post it as such?Cladoceran
B
11
cout << "Enter the number: ";
int number;
if (cin >> number)
{
    // throw away the rest of the line 
    char c;
    while (cin.get(c) && c != '\n')
        if (!std::isspace(c))
        {
            std::cerr << "ERROR unexpected character '" << c << "' found\n";
            exit(EXIT_FAILURE);
        }
    cout << "Enter names: ";
    string name;
    // keep getting lines until EOF (or "bad" e.g. error reading redirected file)...
    while (getline(cin, name))
        ...use name...
}
else
{
    std::cerr << "ERROR reading number\n";
    exit(EXIT_FAILURE);
}

In the code above, this bit...

    char c;
    while (cin.get(c) && c != '\n')
        if (!std::isspace(c))
        {
            std::cerr << "ERROR unexpected character '" << c << "' found\n";
            exit(EXIT_FAILURE);
        }

...checks the rest of the input line after the number contains only whitespace.

Why not just use ignore?

That's pretty verbose, so using ignore on the stream after >> x is an oft-recommended alternative way to discard content through to the next newline, but it risks throwing away non-whitespace content and in doing so, overlooking corrupt data in the file. You may or may not care, depending on whether the file's content's trusted, how important it is to avoid processing corrupt data etc..

So when would you use clear and ignore?

So, std::cin.clear() (and std::cin.ignore()) isn't necessary for this, but is useful for removing error state. For example, if you want to give the user many chances to enter a valid number.

int x;
while (std::cout << "Enter a number: " &&
       !(std::cin >> x))
{
    if (std::cin.eof())
    {
        std::cerr << "ERROR unexpected EOF\n";
        exit(EXIT_FAILURE);
    }

    std::cin.clear();  // clear bad/fail/eof flags

    // have to ignore non-numeric character that caused cin >> x to
    // fail or there's no chance of it working next time; for "cin" it's
    // common to remove the entire suspect line and re-prompt the user for
    // input.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max());
}

Can't it be simpler with skipws or similar?

Another simple but half-baked alternative to ignore for your original requirement is using std::skipws to skip any amount of whitespace before reading lines...

if (std::cin >> number >> std::skipws)
{
    while (getline(std::cin, name))
        ...

...but if it gets input like "1E6" (e.g. some scientist trying to input 1,000,000 but C++ only supports that notation for floating point numbers) won't accept that, you'd end up with number set to 1, and E6 read as the first value of name. Separately, if you had a valid number followed by one or more blank lines, those lines would be silently ignored.

Beaux answered 21/4, 2011 at 5:50 Comment(4)
Erm, this doesn't answer the question or fix the problem. -1Cladoceran
@LightnessRacesinOrbit: how does it "not fix the problem"? The first call to getline consumes the newline after the number and loops until it finds a non-empty line... seems fixed to me. The OP's question didn't ask "why?" the problem happened, but did comment that he wasn't sure why clear might be necessary - someone else edited the 5-year-old question 2 days ago to add that, substantially changing the gist.Beaux
Now you've edited in [someone else's] answer it's betterCladoceran
@LightnessRacesinOrbit: I edited in the gist of my comment on jonsca's answer, from 2011 - nothing new.Beaux
P
26
cout << "Enter the number: ";
int number;
cin >> number;

cin.ignore(256, '\n'); // remaining input characters up to the next newline character
                       // are ignored

cout << "Enter names: ";
string names;
getline(cin, names);
Pablopabon answered 8/7, 2012 at 19:11 Comment(0)
L
20

Another way of doing it is to put a

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

after your cin>>number; to flush the input buffer completely (rejecting all of the extra characters until a newline is found). You need to #include <limits> to get the max() method.

Louvar answered 21/4, 2011 at 5:53 Comment(5)
@jonsca: "rejecting all the extra characters" is dubious in most production systems... fine to eat whitespace, but discarding unknown data can easily and silently lead to wrong results.Beaux
@cnicutar it varies from implementation to implementationLouvar
@Tony How about instead of a getline, if you have a loop taking in characters positioned after the cin statement? Surely the extra characters will throw a wrench into that. Are you talking more about security holes?Louvar
"loop taking in characters"... if you mean while (isspace(cin.peek())) cin.ignore()... looks good to me. Re above, I was thinking more of a user misunderstanding the input format requirements, or some script generating the input breaks, but the broken input seem to be processed successfully because it's ignored - they can end up trusting broken results. If input's not to spec, it's better to have your program generate an error.Beaux
@Tony Ah, okay, I misinterpreted what you were trying to say. Good point about validation.Louvar
B
11
cout << "Enter the number: ";
int number;
if (cin >> number)
{
    // throw away the rest of the line 
    char c;
    while (cin.get(c) && c != '\n')
        if (!std::isspace(c))
        {
            std::cerr << "ERROR unexpected character '" << c << "' found\n";
            exit(EXIT_FAILURE);
        }
    cout << "Enter names: ";
    string name;
    // keep getting lines until EOF (or "bad" e.g. error reading redirected file)...
    while (getline(cin, name))
        ...use name...
}
else
{
    std::cerr << "ERROR reading number\n";
    exit(EXIT_FAILURE);
}

In the code above, this bit...

    char c;
    while (cin.get(c) && c != '\n')
        if (!std::isspace(c))
        {
            std::cerr << "ERROR unexpected character '" << c << "' found\n";
            exit(EXIT_FAILURE);
        }

...checks the rest of the input line after the number contains only whitespace.

Why not just use ignore?

That's pretty verbose, so using ignore on the stream after >> x is an oft-recommended alternative way to discard content through to the next newline, but it risks throwing away non-whitespace content and in doing so, overlooking corrupt data in the file. You may or may not care, depending on whether the file's content's trusted, how important it is to avoid processing corrupt data etc..

So when would you use clear and ignore?

So, std::cin.clear() (and std::cin.ignore()) isn't necessary for this, but is useful for removing error state. For example, if you want to give the user many chances to enter a valid number.

int x;
while (std::cout << "Enter a number: " &&
       !(std::cin >> x))
{
    if (std::cin.eof())
    {
        std::cerr << "ERROR unexpected EOF\n";
        exit(EXIT_FAILURE);
    }

    std::cin.clear();  // clear bad/fail/eof flags

    // have to ignore non-numeric character that caused cin >> x to
    // fail or there's no chance of it working next time; for "cin" it's
    // common to remove the entire suspect line and re-prompt the user for
    // input.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max());
}

Can't it be simpler with skipws or similar?

Another simple but half-baked alternative to ignore for your original requirement is using std::skipws to skip any amount of whitespace before reading lines...

if (std::cin >> number >> std::skipws)
{
    while (getline(std::cin, name))
        ...

...but if it gets input like "1E6" (e.g. some scientist trying to input 1,000,000 but C++ only supports that notation for floating point numbers) won't accept that, you'd end up with number set to 1, and E6 read as the first value of name. Separately, if you had a valid number followed by one or more blank lines, those lines would be silently ignored.

Beaux answered 21/4, 2011 at 5:50 Comment(4)
Erm, this doesn't answer the question or fix the problem. -1Cladoceran
@LightnessRacesinOrbit: how does it "not fix the problem"? The first call to getline consumes the newline after the number and loops until it finds a non-empty line... seems fixed to me. The OP's question didn't ask "why?" the problem happened, but did comment that he wasn't sure why clear might be necessary - someone else edited the 5-year-old question 2 days ago to add that, substantially changing the gist.Beaux
Now you've edited in [someone else's] answer it's betterCladoceran
@LightnessRacesinOrbit: I edited in the gist of my comment on jonsca's answer, from 2011 - nothing new.Beaux
E
10

Try:

int number;

cin >> number;

char firstCharacterOfNames;
cin >> firstCharacterOfNames;  // This will discard all leading white space.
                               // including new-line if there happen to be any.

cin.unget();                   // Put back the first character of the name.

std::string  names;
std::getline(cin, names);      // Read the names;

Alternatively. If you know that number and names will always be on different lines.

cin >> number;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
std::getline(cin, names);
Emu answered 21/4, 2011 at 8:47 Comment(0)
G
8

You can use std::ws to extract any whitespace characters in the input buffer before using getline. Header for std::ws is sstream.

cout << "Enter the number: ";
int number;
cin >> number;
cout << "Enter names: ";
string names;
cin>>ws;
getline(cin, names);
Gutow answered 16/4, 2015 at 17:50 Comment(0)
S
6

Try cin.ignore() when you use cin before getline() function

void inputstu(){
    cout << "Enter roll Number:";
    cin >> roll_no;
    cin.ignore(); //ignore the withspace and enter key
    cout << "Enter name:";
    getline(cin, stu_name);
}
Streptothricin answered 9/12, 2018 at 16:9 Comment(0)
Y
1

Or you can flush the input buffer to read the string

fflush(stdin)

it is defined in header stdio.h.

This code works..

cout << "Enter the number: ";
int number;
cin >> number;

cout << "Enter names: ";
string names;
fflush(stdin);  //FLUSHING STDIN
getline(cin, names);
Yalu answered 12/9, 2016 at 13:17 Comment(0)
W
1

i just used

getline(cin >> ws,lard.i_npute);

with the standard

#include <iostream>

header in the instances where I was having problems with carriage returns and the ws manipulator worked. I will probably start embedding looping functions as classes and using constructor and destructor calls atleast.

Whydah answered 27/10, 2016 at 17:25 Comment(0)
I
1
cout << "Enter the number: ";
int number;
cin >> number;

cout << "Enter names: ";
string names;
getline(cin, names);//works on the \n left behind
getline(cin, names);//continues and rewrites names

its pretty self explainatory, there is a \n left behind in the stream that cin >> number uses, which gets assigned to names the first time its used. Reusing the getline writes the correct value now.

Indolence answered 19/3, 2018 at 17:11 Comment(0)
A
0

You can find the answer you want in cppreference.

When used immediately after whitespace-delimited input, e.g. after int n; std::cin >> n;, getline consumes the endline character left on the input stream by operator>>, and returns immediately. A common solution is to ignore all leftover characters on the line of input with cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); before switching to line-oriented input.

Arguelles answered 20/8, 2016 at 2:17 Comment(0)
R
0

you want to use cin.ignore() after your cin statements because you want to ignore the "\n" left in the buffer after taking your int variable with cin.

i have a similar program i used with a similar problem:

#include <iostream>
#include <iomanip>
#include <limits>

using namespace std;

int main() {
    int i = 4;
    double d = 4.0;
    string s = "HackerRank ";

    // Declare second integer, double, and String variables.
    int n;
    double d2;
    string str;

    // Read and save an integer, double, and String to your variables.
    cin >> n;
    cin >> d2;

    cin.ignore();

    getline(cin, str);

    // Print the sum of both integer variables on a new line.
    cout << i + n << endl;


    // Print the sum of the double variables on a new line.
    cout << d + d2 << endl;

    // Concatenate and print the String variables on a new line
    cout << s << str << endl;

    // The 's' variable above should be printed first.

    return 0;
}
Riane answered 16/9, 2016 at 18:49 Comment(0)
N
0

Conceptually, I think you want each answer to be neatly one line. So why don't you try this?

cout << "Enter the number: ";
string line;
getline(cin, line);
int number = std::stoi(line);

cout << "Enter names: ";
string names;
getline(cin, names);

The code consumes the first newline character correctly, gives you the number if the line is correct or throws an exception if it is not. All for free!

Niedersachsen answered 9/11, 2017 at 12:56 Comment(0)
H
-1
#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "Enter the number: ";
    int number;
    cin >> number;
    cout << "Enter names: ";
    string names;

    // USE peek() TO SOLVE IT! ;)
    if (cin.peek() == '\n') {
        cin.ignore(1 /*numeric_limits<streamsize>::max()*/, '\n');
    }

    getline(cin, names);

    return 0;
}

Just peek ahead using cin.peek() and see if a '\n' is still left in cin's internal buffer. If so: ignore it (basically skip over it)

Himyarite answered 3/2, 2015 at 9:55 Comment(1)
What if it's '\r'? This is a remarkably fragile solution that I would reject in code review.Cladoceran

© 2022 - 2024 — McMap. All rights reserved.