Hi,
I am trying to validate user input from the scanf function. I have managed to ensure the input is an integer between the limits of 2 and 1000, but I haven't managed to get the program to only allow one value at input. Can I do this with scanf of should I be using something different like sscanf?
Here's the piece of code where my problem is:
/*Asks user for an input for the size of the lattice*/
printf("Input size of square lattice: ");
scanf("%d", &row);
/*Input Validation*/
while (valid_input == 0)
{
/*Checks input is only one number*/
if (scanf("%d", &row) == EXPECTED_ARGS)
valid_input = 1;
else
{
printf("Only one value required to determine square lattice size\n");
printf("Enter a single value to determine the size of square lattice: ");
scanf("%d", &row);
}
while (exit_flag == 0)
{
/*Checks input is in the accepted range and is an integer*/
if ((row <= MAX_NUMBER) && (row >= MIN_NUMBER))
exit_flag = 1;
else
{
valid_input = 0;
printf("Lattice must be an integer between 2 and 1000\n");
printf("Enter new lattice size in this range: ");
scanf("%d", &row);
}
}
}