I'm trying to ask the user to enter one integer value: 1,2,3 or 4.
But if the user enters 2a or 2p for example, then the program proceeds with choice 2. How do I fix this error?
Below is the part of my codes.
int main()
{
//Declare variables
int scores[SIZE];
//Read data from file and retrieve scores
int size = readData(scores);
if (size>0)
{
bool flag = true;
If you run that, and then enter something like '2a', you'll get 2 as your output.
In your case, your best bet might be getting your input in a string format first. Validate the input there, i.e. assert there's no non-numerical characters. After validation, you can convert your string to an integer. This can be done with std::stringstream or something like std::stoi.