Search code examples
cloopsif-statementwhile-loopuser-input

How to keep asking user to input until condition is satisfied in C?


This is small C program and I am trying to validate the user input. I want user to enter 3 values. Condition is that all 3 inputs should not be of same value. So how to loop the user input until condition is satisfied(True). Here is the code. Help me out, I am new to Programming, Thanks. :)

#include<stdio.h>

int main()
{
    int u1,u2,u3;
    
    printf("Enter 3 Numbers : ");  //Printing
    scanf("%d %d %d",&u1,&u2,&u3); //Asking for Input
    
    if(u1==u2 || u2==u3 || u3==u1)       //This is the condition
    {
        printf("Condition is not Satisfied !");      
    }
}

So how do I loop it. Thank You.


Solution

  • I recommend that you use the following code:

    #include <stdio.h>
    
    int main( void )
    {
        int u1,u2,u3;
    
        for (;;) //infinite loop, equivalent to while(true)
        {
            printf( "Enter 3 Numbers: " );
            scanf( "%d %d %d", &u1, &u2, &u3 );
    
            if ( u1!=u2 && u2!=u3 && u3!=u1 )
                break;
    
            printf( "Error: Condition is not satisfied!\n" );
        }
    }
    

    In contrast to one of the other answers, this solution has the advantage that it only checks the condition once per loop iteration.

    However, the above code (and the code of most other answers) has a serious issue: If the user enters an alphabetical letter instead of a number, the program will get stuck in an infinite loop. This is because it is not safe to call scanf without checking the return value. See the following page for more information on why it is unsafe: A beginners' guide away from scanf()

    Therefore, it would be better to check the return value of scanf and to consume all characters on the remainder of the line. Consuming all leftover characters is important, because otherwise, if the user enters 6abc, then scanf will consume the 6, but leave abc on the input stream, so that the next time scanf is called (which will be in the next loop iteration), it will attempt to match abc and fail immediately without waiting for further input. This would cause an infinite loop.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void )
    {
        int u1,u2,u3;
        int ret;
    
        for (;;) //infinite loop, equivalent to while(true)
        {
            printf( "Enter 3 Numbers: " );
            if ( ( ret = scanf( "%d %d %d", &u1, &u2, &u3 ) ) != 3 )
            {
                int c;
    
                //check for unrecoverable error
                if ( ret == EOF )
                {
                    fprintf( stderr, "Unrecoverable input error!\n" );
                    exit( EXIT_FAILURE );
                }
    
                //print error message for recoverable error
                printf( "Unable to convert input!\n" );
    
                //consume all leftover characters on the line
                do
                {
                    c = getchar();
    
                } while ( c != EOF && c != '\n' );
            }
    
            if ( u1!=u2 && u2!=u3 && u3!=u1 )
                break;
    
            printf( "Error: Condition is not satisfied!\n" );
        }
    }