问题
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?
回答1:
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);
回答2:
You need to flush the input buffer. It can be done with cin.clear(); cin.sync();
.
回答3:
You can use
cin.ignore();
or as @kernald mentioned use
cin.clear();
cin.sync();
before using getline()
回答4:
Use cin.clear()
as mentioned and use proper error handling:
cin.clear();
cin.sync();
cout << "Title: ";
if (!getline(cin, inputString)) exit 255;
tempDVD.setTitle(inputString);
cout << "Category: ";
if (!getline(cin, inputString)) exit 255;
tempDVD.setCategory(inputString);
cout << "Duration (minutes): ";
if (!(cin >> inputUInt)) exit 255;
tempDVD.setDuration(inputUInt);
cout << "Year: ";
if (!(cin >> inputUInt)) exit 255;
tempDVD.setYear(inputUInt);
cout << "Price: $";
if (!(cin >> inputDouble)) exit 255;
tempDVD.setPrice(inputDouble);
回答5:
If user inputs a space before \n in previous cin before getline, only ignore itself wouldn't be enough so you have to use this code instead of ignore() alone. For example 12345 \t \n will not work anymore. All unprocessed characters must be ignored.
#include <limits>
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Use this between cin and getline.
回答6:
Mixing getline() with input streams in generally a bad thing to do. It's theoretically possible to manually handle the dirty buffers left over by using streams, but it's an unnecessary pain that should definitely be avoided.
You are better off using a console library to grab your input, this way the dirty-work can be abstracted for you.
Take a look at TinyCon. 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.
You can find information here: https://sourceforge.net/projects/tinycon/
回答7:
cin.sync(); use this instead of cin.ignore( works best.
来源:https://stackoverflow.com/questions/7786994/c-getline-isnt-waiting-for-input-from-console-when-called-multiple-times