I am not sure how to describe this problem, so I'll just keep it simple.
In my console app, users can choose if they want to exit the program or not by entering 'Y' or 'N'.
This works fine, but whenever the user enters more characters, it starts to act weird.
For example, if a user enters 'hello' where he/she should've put 'Y' or 'N', the output is as follows:
Would you like to exit the program? [N/Y]
Option: hello
Invalid option - please enter a valid value. Variable holds: h
Would you like to exit the program? [N/Y]
Option: Invalid option - please enter a valid value. Variable holds: e
Would you like to exit the program? [N/Y]
Option: Invalid option - please enter a valid value. Variable holds: l
Would you like to exit the program? [N/Y]
Option: Invalid option - please enter a valid value. Variable holds: l
Would you like to exit the program? [N/Y]
Option: Invalid option - please enter a valid value. Variable holds: o
Would you like to exit the program? [N/Y]
Option:
The code is as follows:
1 2 3 4 5 6 7 8 9 10
end:
std::cout << "Would you like to exit the program? [N/Y]\n";
char exit;
std::cout << "Option: ";
std::cin >> exit;
if (exit == 'y') { return 0; }
elseif (exit == 'n') { goto keyoption; }
else {
std::cout << "Invalid option - please enter a valid value. Variable holds: " << exit << "\n";
goto end;
Any suggestions on how to fix this?
Thanks in advance.