Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ getline() isn't waiting for input from console when called multiple times

Tags:

c++

getline

I'm attempting to get a few user-input parameters from the console, two strings, two ints and a double. The relevant code I'm trying to use is this:

#include <string> #include <iostream> using namespace std;  // ...  string inputString; unsigned int inputUInt; double inputDouble;  // ...  cout << "Title: ";  getline(cin, inputString); tempDVD.setTitle(inputString);  cout << "Category: ";  getline(cin, inputString); tempDVD.setCategory(inputString);  cout << "Duration (minutes): ";  cin >> inputUInt;  tempDVD.setDuration(inputUInt);  cout << "Year: ";  cin >> inputUInt;  tempDVD.setYear(inputUInt);  cout << "Price: $";  cin >> inputDouble;  tempDVD.setPrice(inputDouble); 

However, when running the program, instead of waiting for the first inputString to be entered, the code doesn't stop until the second getline() call. Thus the console output looks like this:

Title: Category:

with the cursor appearing after category. If I input now, the program then jumps ahead to the year input, not allowing me to enter more than one string. What's happening here?

like image 608
user754852 Avatar asked Oct 16 '11 20:10

user754852


People also ask

Does Getline wait for input?

std::cin leaves the newline character in the buffer after pressing enter, and getline just grabs it and keeps going, that's why getline doesn't block to wait for input. cin>> will leave newlines in the buffer when the user presses enter. getline() reads this as the user having pressed enter to "skip" the input.

Can we use Getline for multiple times in a C++ program?

You can just use the static method tinyConsole::getLine() in replace of your getline and stream calls, and you can use it as many times as you'd like. Show activity on this post. cin. sync(); use this instead of cin.

Why CIN Getline is not working?

Problem with getline() after cin >> The getline() function does not ignore leading white space characters. So special care should be taken care of about using getline() after cin because cin ignores white space characters and leaves it in the stream as garbage.

Can CIN Getline take multiple lines as input?

The cin is an object which is used to take input from the user but does not allow to take the input in multiple lines. To accept the multiple lines, we use the getline() function.


1 Answers

The problem is you are mixing calls to getline() with the use of the operator >>.

Remember that operator >> ignored leading white space so will correctly continue across lines boundaries. But stops reading after the input has successfully been retrieved and thus will not swallow trailing '\n' characters. Thus if you use a getline() after a >> you usually get the wrong thing unless you are careful (to first remove the '\n' character that was not read).

The trick is to not use both types of input. Pick the appropriate one and stick to it.

If it is all numbers (or objects that play nice with operator >>) then just use operator >> (Note string is the only fundamental type that is not symmetric with input/output (ie does not play nicely)).

If the input contains strings or a combination of stuff that will require getline() then only use getline() and parse the number out of the string.

std::getline(std::cin, line); std::stringstream  linestream(line);  int  value; linestream >> value;  // Or if you have boost: std::getline(std::cin, line); int  value = boost::lexical_cast<int>(line); 
like image 184
Martin York Avatar answered Oct 01 '22 13:10

Martin York