Thread: Numeric input validation in loops

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    5

    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. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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)
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    It might be worth looking into switch statements

    Switch Case in C - Cprogramming.com

    Have a close look at the last example
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    5
    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. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    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. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    5
    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. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    5
    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. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-11-2015, 04:12 PM
  2. Replies: 5
    Last Post: 04-06-2015, 09:39 AM
  3. input validation
    By Alexius Lim in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2013, 04:42 AM
  4. Formatted input: Detecting numeric overflow?
    By Memloop in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2010, 03:51 AM
  5. Reading numeric input one by one
    By oval in forum C Programming
    Replies: 10
    Last Post: 01-24-2006, 03:45 PM

Tags for this Thread