Mar 8, 2014 at 12:36pm UTC
I have this:
1 2 3 4 5 6 7 8 9
string input;
unsigned short choice;
...
istringstream valid(input);
...
if (!(valid >> choice))
{
//some error
}
Ok. My code is almost 1000 lines, and I have splited some functions in headers. But the same function doesn't work:
1 2 3 4 5
template <typename T> bool valid_input(const string& input, T var)
{
istringstream valid(input);
return (valid >> var);
}
You can check it here:
http://coliru.stacked-crooked.com/a/0f22981e72360f6f
The output is correct, but in my machine with C++11, MinGW 4.8 (64 bit in a 64bit-Windows8), the output is incorrect. Why?
If you want more specific info, the problem is that I use input, I think. I use std::getline(std::cin, some_string).
Last edited on Mar 8, 2014 at 12:45pm UTC
Mar 8, 2014 at 1:00pm UTC
Is your program fails on same test you posted on Coliru? Because there should not be errors, and it works with 64bit MinGW GCC 8.8.1 compiler on 64bit Win7.
Mar 8, 2014 at 2:27pm UTC
No, it doesn't. By that I think that the error appears because of the input...
Mar 8, 2014 at 4:30pm UTC
Shouldn't it be
template <typename T> bool valid_input(const string& input, T& var) // <-- T&, not T
?