¿what are you doing? `cin' is not in an invalid state because you used the string stream, you are ignoring the next line of input, and the stringstream will be created again in line 12.
I have never used an istringstream before, I will have to look that up.
As for the later code:
You are totally right. I had adapted the code using stringstream instead of cin. I really should stop coding after 10PM. Tired eyes completely swept through that thanks for the input.
However in it's defense the program worked as intended every time I tested it. My main wonder of this code wasn't much it's design, as I stated I would clean it up in the morning. However curiosity to the using of goto in this particular case being necessarily bad code? I'm not sure if would simply translate to an assembly jmp instruction, making it slightly more efficient to a function call in this kind of case? These kind of things were almost always necessary when programming at a lower level. I'm just asking myself if the simplicity of goto could be used correctly in certain cases, rather than creating an entirely new function.
EDIT:
Thanks for the tips ne555, nice check on checking if the stream as reached eof. Nice one (Y).
Much better:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
template<typename T>
T GetConsoleInput(std::string s)
{
T value;
while(1)
{
std::cout << s; // Display prompt message
std::string input;
std::getline(std::cin,input); // Get user input
std::istringstream ss(input);
if(ss >> value && ss.eof()) return value;
else std::cout << "Invalid input.\n";
}
}