So why is it that the majority of the time I want cin to behave a certain way, and I think a lot of other people probably want it to behave that way as well. The thing that's bothering me is that, not only is it implemented differently, but there's not even stream manipulators to make it behave the way people want it to.
What am I talking about?
Well...
I wrote this:
template <typename T>
T getUserValue(const string& requestString) {
T retval;
while(true) {
cout << requestString;
cin >> retval;
bool ok = cin.good();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //whyyyyyy
if(ok) {return retval;}
cout << "Invalid input." << endl;
}
}
Ignoring the annoyance of cin.ignore to discard the newline character, this works just fine for grabbing ints and floats. It works just fine for grabbing a std::string, UNLESS the string has a space in it - and there's my main problem.
So what I want to know is...
Why aren't there manipulators that solve these common problems? Why can't I just say:
template <typename T>
T getUserValue(const string& requestString) {
T retval;
while(true) {
cout << requestString;
cin >> allow_spaces >> consume_eol >> retval;
if(cin.good()) {return retval;}
cin.clear();
cout << "Invalid input." << endl;
}
}
Now I'm not asking you to tell me about std::getline(). I understand that I can get lines that way. I'm just wondering why cin is so clumsy, having all these pitfalls in the face of common, expected usage, and doesn't have the obvious fixes available to it.
Am I missing something here?