Thread: being confused about a program segment

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    12

    being confused about a program segment

    if i enter unexpectedly a letter(for example 'v' )-at the XXX i posted above-, the program goes to second if segment; BUT; it does not wait to enter 'c ' or 'e ' after printing the Sorry,WRONG...etc. and it terminates. When i enter;
    ---> 'e' : it ends
    ---> 'c' : it continues
    ---> 'any' : it ends
    in all cases,why does the program continue...?
    Attached Files Attached Files
    • File Type: c zzz.c (1.3 KB, 101 views)

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    First, please post your code here on the forum, in code tags, and make sure it is neatly indented:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int display_question();
    static int result;
    
    int main()
    {
        char a;
        int b,c,d,e,f;
    
        srand(time(NULL));
    
        printf("WELCOME GAME!!!\n");
        a='c';
    
        display_question();
    
        printf("Now,please enter your answer");
        scanf("%d",&f);
    
        if (f != result)
        {
            printf("c or e");
            scanf(" %c",&a);
            if (a=='e')
                return 1;
        }
    
        while (a=='c')
        {
            display_question();
            printf("Now,please enter your answer\n");
            scanf(" %d",&f);
            printf("RESULT IS ----> %d\n",f);
    
            if (f==result)
            {
                printf("TRUE..press 'c' to continue or 'e' to end\n");
                scanf(" %c",&a);
                if (a=='e')
    			{
                    printf("GOOD BYE!!!\n");
                    return 1;
    			}
    		}
    
            if (f != result)
    		{
                printf("Sorry,WRONG ANSWER..press 'c' to continue or 'e' to end\n");
                scanf(" %c",&a);
    
                if ( a == 'e')
    			{
                    printf("GOOD BYE!\n");
                    return 1;
    			}
    
    		}
        }
    
        return 0;
    }
    
    int display_question()
    {
        int x,y,z;
    
        x = 1+rand() % 100;
        y =  1+rand() % 100;
        z =1+ rand() % 4; // produce 1,2,3,4...
    
        switch(z)
        {
        case 1:
            {
            printf("What is the result of ---> # %d * %d ? #\n",x,y);
            result = x*y;
            break;
            }
        case 2:
            {
            printf("What is the result of ---> # %d / %d ? #\n",x,y);
            result = x/y;
            break;
            }
        case 3:
            {
            printf("What is the result of ---> # %d + %d ? #\n",x,y);
            result = x+y;
            break;
            }
        case 4:
            {
            printf("What is the result of ---> # %d - %d ? #\n",x,y);
            result = x-y;
            break;
            }
        }
    
        return 0;
    }
    "scanf()" tries to read the type you tell it to expect ... but if you enter a type it doesn't expect (such as entering a character when an integer is expected), it will fail to read the input, and the input will remain in the buffer. This could mess up further input reads, as it might continue to try to read input that is not expected.

    Fortunately, the "scanf()" function returns the number of item successfully read. So you can use this as a rudimentary form of input validation (though more robust input validation is strongly recommended).

    For an example, run the following program:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x;
        int result;
    
        printf("Enter an integer:\n");
    
        result = scanf("%d",&x);
    
        if(result == 1)
            printf("You entered the integer: %d\n",x);
        else
            printf("You did not enter an integer\n");
    
        return 0;
    }
    The first time, enter a number (e.g. 34).
    The second time, enter a character (e.g. the letter 'v').

    Compare the output of each run, and notice how the return value of "scanf()" is checked.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    12
    I am happy to understand and it is really nice solution.,Thank you so much for replying !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ciphering data segment of program
    By fnoyan in forum C Programming
    Replies: 1
    Last Post: 06-01-2013, 06:08 AM
  2. Segment fault in the program
    By sanddune008 in forum C Programming
    Replies: 6
    Last Post: 04-27-2012, 11:11 AM
  3. segment fault with the program
    By sanddune008 in forum C++ Programming
    Replies: 4
    Last Post: 03-25-2011, 10:23 AM
  4. program that segment faults..
    By sanddune008 in forum C Programming
    Replies: 12
    Last Post: 04-28-2010, 02:48 AM
  5. Looking for feedback on program segment
    By avron in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 04:38 PM