Thread: Problem with input validation(no alphabet allowed) and loop

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    4

    Exclamation Problem with input validation(no alphabet allowed) and loop

    Requirement:
    1. ask user to input value for x,y,z
    2. check the value is not alphabet
    3. loop when user wish to continue

    Problem:
    1. When the input is all numeric it still will display the error message in the alphabet checking code and stop.
    Example:
    Enter real number X: 1
    Enter real number Y: 1
    Enter real number Z: 1
    Entered value contained alphabet/symbol

    2. If the above successful, i wish to allow user to decide want to continue or not. I tried and it not looping.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    
    main ()
    {
    
    
        float X,Y,Z;
        float rsltA = 0;
        float rsltB = 0;
        float rsltC= 0;
        int i=0;
        char str[10];
        char choice;
        
        
        do
        {
                printf("Enter real number X: ");
                scanf("%f",&X);
            
                printf("Enter real number Y: ");
                scanf("%f",&Y);
                
                printf("Enter real number Z: ");
                scanf("%f",&Z);
            
            //while (scanf("%f%f%f", &X,&Y,&Z) != 1)
            //{
        
                fgets(str,sizeof(str),stdin);
        
                while(i<strlen(str))
                    {
            
                        if(str[i]<48||str[i]>57)
                        {
                            printf("Entered value contained alphabet/symbol\n");
                            getchar();
                            return 0;
                            
                        }
                    
                        else
                        {
    
    
                        //display the value for X,Y and Z
                            printf("X=%.4f ; Y=%.4f ; Z=%.4f\n", X, Y, Z );
                            
                            
                            //mathematic equations
                            rsltA= (5*powf(X, 2)+(3*Y))*((4*X)-(5*Y));
                            rsltB= logf(Z)/powf((2-logf(X)),2);
                            rsltC= ((4*rsltA)+(3*rsltB))/(powf(rsltB,2));
                            
                            
                            //condition for X,Y and Z
                            if (X==0 && Z==0)
                            {
                                printf("Function g(x,z) is not defined at x=0.0000, z=0.0000\n");
                                printf("f(x,y): %.4f; ",rsltA);
                                printf("g(x,z):#INF; ");
                                printf("g(x,y,z):#UNDEF\n");
                                printf("Do you want to continue Y/N: ");
                                scanf(" %c", &choice);
                            }
                            
                            if (X<0 && Z<0)
                            {
                                printf("Function g(x,z) is not defined at x=%.4f, z=%.4f\n",X,Z);
                                printf("f(x,y): %.4f; ",rsltA);
                                printf("g(x,z):#INF; ");
                                printf("g(x,y,z):#UNDEF\n");
                                printf("Do you want to continue Y/N: ");
                                scanf(" %c", &choice);
                            }
                            
                            if ((X < 0 )||(X == 0))
                            {
                                printf("Function g(x,z) is not defined at x=%.4f\n",X);
                                printf("f(x,y): %.4f; ",rsltA);
                                printf("g(x,z):#INF; ");
                                printf("g(x,y,z):#UNDEF\n");
                                printf("Do you want to continue Y/N: ");
                                scanf(" %c", &choice);
                            }
                            /*
                             if (X+=0.01)
                             {
                             printf("Function g(x,z) is not defined at x=0.0100\n");
                             
                             }
                             
                             
                             if ((X += 0.01) && (Z <= 0))
                             {
                             printf("Function g(x,z) is not defined at x=0.0100, z=%.4f\n",Z);
                             
                             }
                             
                             if ((X += 0.01) && Z > 0)
                             {
                             
                             printf("Function g(x,z) is not defined at x=0.0100\n");
                             
                             
                             }*/
                            
                            if ((Z < 0 )||(Z == 0))
                            {
                                printf("Function g(x,z) is not defined at z=%.4f\n",Z);
                                printf("f(x,y): %.4f; ",rsltA);
                                printf("g(x,z):#INF; ");
                                printf("g(x,y,z):#UNDEF\n");
                                printf("Do you want to continue Y/N: ");
                                scanf(" %c", &choice);
                            }
                            else
                            {
                                
                                printf("f(x,y): %.4f; ", rsltA);
                                printf("g(x,z): %.4f; ", rsltB);
                                printf("h(x,y,z): %.4f \n", rsltC);
                                printf("Do you want to continue Y/N: ");
                                scanf(" %c", &choice);
                            }
                        }i++;
                    //}
            }
        }while(choice=='Y'|| choice=='y');
                    
            
        
        printf("Thanks you");
        
    
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    scanf() (line 29) leaves the ENTER in the input buffer.
    fgets() (line 34) reads that ENTER to the first position in str.
    The subsequent test fails.

    Do not mix scanf() and fgets().

    Ignore the "check the value is not alphabet". scanf() already does that on its own. If the input cannot be interpreted as a float, scanf() returns a number less than the expected assignments.

    Code:
        if (scanf("%f", &X) != 1) /* error */;
        while (scanf("%f%f%f", &X, &Y, &Z) == 3) { /* loop */ }

  3. #3
    Registered User
    Join Date
    Aug 2016
    Posts
    4
    How to join the scanf() after the printf() to the one in the while()
    It true that when there is non numeric input it will skip to the end
    but if wish to check whether user want to re-input the value the looping seems not quite right
    Last edited by JiaWei Lee; 08-30-2016 at 08:10 AM.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    The best way to deal with user input (in respect to error checking, validation, recovery) is to read full lines with fgets(), then parse those lines with sscanf().
    This requires a bit more work than doing it all with scanf() (see example below).

    Code:
        while (1) {
            if (scanf("%f%f%f", &x, &y, &z) != 3) /* error */;
            // deal with x, y, and z
            printf("Continue (Y/N)? ");
            scanf(" %c", &choice);
            if ((choice != 'Y') && (choice != 'y')) break; // exit infinite while loop
        }

  5. #5
    Registered User
    Join Date
    Aug 2016
    Posts
    4
    Tried it but it become can input infinite without execute other code. when key in 'y' after the scanf() for choice it will just loop the printf() continue statement.
    can key in infinite number when the input is integer
    Enter real number X:
    Enter real number Y:
    Enter real number Z:
    1
    1
    1
    1

    when key in alphabet code keep looping until printf() continue
    Enter real number X:
    Enter real number Y:
    Enter real number Z:
    Entered value is not numeric
    Continue (Y/N)? y
    Enter real number X:
    Enter real number Y:
    Enter real number Z:
    Entered value is not numeric
    Continue (Y/N)?


    after that tried to input integer then alphabet then integer somehow it can run to the end

    Enter real number X:
    Enter real number Y:
    Enter real number Z:
    Entered value is not numeric
    Continue (Y/N)? 1
    1
    1
    1
    1
    1
    f
    Entered value is not numeric
    Continue (Y/N)? 1
    1
    1
    X=1.0000 ; Y=1.0000 ; Z=1.0000
    f(x,y): -8.0000; g(x,z): 0.0000; h(x,y,z): -inf
    Do you want to continue Y/N:


    Code:
    do
        {
            printf("Enter real number X: \n");
            //scanf("%f",&X);
            
            printf("Enter real number Y: \n");
            //scanf("%f",&Y);
            
            printf("Enter real number Z: \n");
            //scanf("%f",&Z);
            
             while(1)
            {
                if (scanf("%f%f%f", &X,&Y,&Z) != 3)
                {
                printf("Entered value is not numeric\n");
                printf("Continue (Y/N)? ");
                scanf(" %c", &choice);
                }
                if ((choice != 'Y') && (choice != 'y')) break;
            }
            
                if(scanf("%f%f%f", &X, &Y, &Z) == 3)
                {
                
       //continue to execute
                }
    Last edited by JiaWei Lee; 08-31-2016 at 07:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input validation problem
    By loveable in forum C Programming
    Replies: 6
    Last Post: 12-30-2012, 07:10 AM
  2. Input validation problem
    By Huncowboy in forum C Programming
    Replies: 3
    Last Post: 01-28-2010, 08:27 AM
  3. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  4. Validation loop problem
    By ManiacBR in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2006, 12:09 PM
  5. Input validation loop failure
    By MacNilly in forum C++ Programming
    Replies: 4
    Last Post: 03-01-2006, 03:29 AM

Tags for this Thread