# Thread: Numeric input validation in loops

1. ## Numeric input validation in loops

Hi everyone, Im new to this forum and taking a C programming course. I am having trouble here validating that the user does not input any negatives or any value or 10. But I can't seem to make it work right.

Code:
```#include <stdio.h>
#include <stdlib.h>

int main (void)
{
system("clear");
int input;
int choice; //Allow the user to enter Y or N
choice = 0;

do while (choice == 0)
{
printf("Please enter a number between 1 and 10 to convert: \n");
scanf("%d", &input);
printf("--------------------------- \n");

if(input <= 1 || input <= 10)
{
printf("Please enter a number between 1 and 10 \n");\
scanf("%d", &input);
printf("--------------------------- \n");
}

else if(input == 1)
{
printf("The number 1 is I in roman numerals \n");
}

else if(input == 2)
{
printf("The number 2 is II in roman numerals \n");
}

else if(input == 3)
{
printf("The number 3 is III in roman numerals \n");
}

else if(input == 4)
{
printf("The number 4 is IV in roman numerals \n");
}

else if(input == 5)
{
printf("The number 5 is V in roman numerals \n");
}

else if(input == 6)
{
printf("The number 6 is VI in roman numerals \n");
}

else if(input == 7)
{
printf("The number 7 is VII in roman numerals \n");
}

else if(input == 8)
{
printf("The number 8 is VIII in roman numerals \n");
}

else if(input == 9)
{
printf("The number 9 is IX in roman numerals \n");
}

else if(input == 10)
{
printf("The number 10 is X in roman numerals \n");
}

//Ask the user if they want to play again.
printf("Would you like to play again? 1 = Yes, 2 = No \n");
printf("--------------------------- \n");
scanf(" %d", &choice);

if (choice == 1)
{
printf("Please enter a number between 1 and 10 to convert: \n");
scanf("%d", &input);
printf("--------------------------- \n");

if(input <= 1 || input <= 10)
{
printf("Please enter a number between 1 and 10 \n");\
scanf("%d", &input);
printf("--------------------------- \n");
}

if(input == 1)
{
printf("The number 1 is I in roman numerals \n");
}

else if(input == 2)
{
printf("The number 2 is II in roman numerals \n");
}

else if(input == 3)
{
printf("The number 3 is III in roman numerals \n");
}

else if(input == 4)
{
printf("The number 4 is IV in roman numerals \n");
}

else if(input == 5)
{
printf("The number 5 is V in roman numerals \n");
}

else if(input == 6)
{
printf("The number 6 is VI in roman numerals \n");
}

else if(input == 7)
{
printf("The number 7 is VII in roman numerals \n");
}

else if(input == 8)
{
printf("The number 8 is VIII in roman numerals \n");
}

else if(input == 9)
{
printf("The number 9 is IX in roman numerals \n");
}

else if(input == 10)
{
printf("The number 10 is X in roman numerals \n");
}

//Ask the user if they want to play again.
printf("Would you like to play again? 1 = Yes, 2 = No \n");
printf("--------------------------- \n");
scanf(" %d", &choice);
choice = 0;
}

else if (choice == 2)
{
break;
}
}
while (choice == 0);

return 0;
}```

2. Review how do/while loops work For, While and Do While Loops in C - Cprogramming.com

Hint: This line is not normal!
Code:
`do while (choice == 0)`

3. It might be worth looking into switch statements

Switch Case in C - Cprogramming.com

Have a close look at the last example

4. I totally forgot about switches even thought I just reviewed them in a videolab. Now I made my code less chunky looking and better. Just that after playing again, when asked to play again and you press no. it hangs and exits on any input. how do I fix that? Here is my new code.

Code:
```#include <stdio.h>
#include <stdlib.h>

void convert()
{

system("clear");
int input;
int choice; //Allow the user to enter Y or N
choice = 0;

printf("Please enter a number between 1 and 10 to convert: \n");
scanf("%d", &input);
printf("--------------------------- \n");

switch (input)
{
case 1:
printf("The number 1 is I in roman numerals \n");
printf("--------------------------- \n");
break;
case 2:
printf("The number 2 is II in roman numerals \n");
printf("--------------------------- \n");
break;
case 3:
printf("The number 3 is III in roman numerals \n");
printf("--------------------------- \n");
break;
case 4:
printf("The number 4 is IV in roman numerals \n");
printf("--------------------------- \n");
break;
case 5:
printf("The number 5 is V in roman numerals \n");
printf("--------------------------- \n");
break;
case 6:
printf("The number 6 is VI in roman numerals \n");
printf("--------------------------- \n");
break;
case 7:
printf("The number 7 is VII in roman numerals \n");
printf("--------------------------- \n");
break;
case 8:
printf("The number 8 is VIII in roman numerals \n");
printf("--------------------------- \n");
break;
case 9:
printf("The number 9 is IX in roman numerals \n");
printf("--------------------------- \n");
break;
case 10:
printf("The number 10 is X in roman numerals \n");
printf("--------------------------- \n");
break;
default:
printf( "Please input a non-negative number or one less than 10! \n" );
printf("--------------------------- \n");
break;
}

//Ask the user if they want to play again.
printf("Would you like to play again? 1 = Yes, 2 = No \n");
printf("--------------------------- \n");
getchar();
scanf(" %d", &choice);

switch ( choice )
{
case 1:
convert();
break;
case 2:
break;
}
getchar();

return 0;
}

int main()
{
system("clear");
int input;
int choice; //Allow the user to enter Y or N
choice = 0;

printf("Please enter a number between 1 and 10 to convert: \n");
scanf("%d", &input);
printf("--------------------------- \n");

switch (input)
{
case 1:
printf("The number 1 is I in roman numerals \n");
printf("--------------------------- \n");
break;
case 2:
printf("The number 2 is II in roman numerals \n");
printf("--------------------------- \n");
break;
case 3:
printf("The number 3 is III in roman numerals \n");
printf("--------------------------- \n");
break;
case 4:
printf("The number 4 is IV in roman numerals \n");
printf("--------------------------- \n");
break;
case 5:
printf("The number 5 is V in roman numerals \n");
printf("--------------------------- \n");
break;
case 6:
printf("The number 6 is VI in roman numerals \n");
printf("--------------------------- \n");
break;
case 7:
printf("The number 7 is VII in roman numerals \n");
printf("--------------------------- \n");
break;
case 8:
printf("The number 8 is VIII in roman numerals \n");
printf("--------------------------- \n");
break;
case 9:
printf("The number 9 is IX in roman numerals \n");
printf("--------------------------- \n");
break;
case 10:
printf("The number 10 is X in roman numerals \n");
printf("--------------------------- \n");
break;
default:
printf( "Please input a non-negative number or one less than 10! \n" );
printf("--------------------------- \n");
break;
}

//Ask the user if they want to play again.
printf("Would you like to play again? 1 = Yes, 2 = No \n");
printf("--------------------------- \n");
scanf(" %d", &choice);

switch ( choice )
{
case 1:
convert();
break;
case 2:
break;
}
getchar();

return 0;
}```

5. scanf() returns the number of successful convertions made or EOF (-1) in case of error. Keep in mind that stdin is buffered so, if you enter something like "10 20", scanf() will not wait for input in the second iteraction of the loop (if you use one)... In your case, you could do:

Code:
` if ( scanf( "%d", &input ) != 1 ) { fputs( "Input must be an integer.\nAborted.\n", stderr ); exit(1); }`

6. I reworked it and this seems to work.

Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{

system("clear");
int input;
int choice; //Allow the user to enter Y or N
choice = 0;

LOOP:do
{

printf("Please enter a number between 1 and 10 to convert: \n");
scanf("%d", &input);
printf("--------------------------- \n");

switch (input)
{
case 1:
printf("The number 1 is I in roman numerals \n");
printf("--------------------------- \n");
break;
case 2:
printf("The number 2 is II in roman numerals \n");
printf("--------------------------- \n");
break;
case 3:
printf("The number 3 is III in roman numerals \n");
printf("--------------------------- \n");
break;
case 4:
printf("The number 4 is IV in roman numerals \n");
printf("--------------------------- \n");
break;
case 5:
printf("The number 5 is V in roman numerals \n");
printf("--------------------------- \n");
break;
case 6:
printf("The number 6 is VI in roman numerals \n");
printf("--------------------------- \n");
break;
case 7:
printf("The number 7 is VII in roman numerals \n");
printf("--------------------------- \n");
break;
case 8:
printf("The number 8 is VIII in roman numerals \n");
printf("--------------------------- \n");
break;
case 9:
printf("The number 9 is IX in roman numerals \n");
printf("--------------------------- \n");
break;
case 10:
printf("The number 10 is X in roman numerals \n");
printf("--------------------------- \n");
break;
default:
printf("Please enter a non-negative number or less than 10! \n");
printf("--------------------------- \n");
break;

}

//Ask the user if they want to play again.
printf("Would you like to play again? 0 = Yes, 1 = No \n");
printf("--------------------------- \n");
getchar();
scanf(" %d", &choice);

}
while( choice == 0 );

return 0;
}```

7. Also I added this in this switch

Code:
```    default:
printf ("Please enter a non-negative number or less than 10! \n");
printf ("--------------------------- \n");
goto LOOP;
break;```

8. There are a few arguably acceptable uses for goto in C, but forcing a loop isn't one of them. In this case, continue might do the trick instead, but a better way is to change the reading of the number into a loop where you perform input validation and hence keep looping if the number is not between 1 and 10.