1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C- if problem

Discussion in 'C' started by visokeeg, Oct 21, 2008.

  1. visokeeg

    visokeeg New Member

    Joined:
    Oct 21, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I want it to check if answer enter by user is correct or wrong. The answer to the question is 'b'...but when entering B it keeps prints WRONG which suppose to print CORRECT!...the questions are on a text file.

    heres the code

    Code:
    
    
    char c; /*variable to hold character input by user*/
        char choice[3]; /*create character array*/
        int correct=0;
        int wrong=0;
        int i=0; /*initialize counter i*/
        
        
         printf( "\n\n\nENTER YOUR ANSWER HERE: "); 
           
           /*use getchar to read choice*/
           while(( c = getchar()) !='\n'){
                              choice[i++]=c;
                               }/*end while*/
                               
           choice[i]='\0'; /*terminate string*/
           
         if(choice[c] == 'b'){
            correct=correct+1;
                   printf("\n------------------------------------------------------------------------------\n");
                   printf("\n\n                ANSWER IS CORRECT");
                   printf("\n------------------------------------------------------------------------------\n");
                   }
         else{
           wrong=wrong+1;
              printf("\n------------------------------------------------------------------------------");
              printf("\n                             WRONG!!!");
              printf("\n------------------------------------------------------------------------------\n");
              }
         
              
            printf("\n\n\n\n\n\n\n\npress any 'ENTER' for next Questions: ");
             while(!getchar());          
             system("cls"); 
           
           return 0;
            }
    
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Have a close look at the "if" statement.
    Display the left hand side with a printf if it'll help you determine what's wrong.
     
  3. visokeeg

    visokeeg New Member

    Joined:
    Oct 21, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    aight this work but when I compile it and press the any key for the answer, it exits the program...any sugggestion how to fix it?



    Code:
    
    char c; /*variable to hold character input by user*/
        char choice[3]; /*create character array*/
        int correct=0;
        int wrong=0;
        int i=0; /*initialize counter i*/
        
        
         printf( "\n\n\nENTER YOUR ANSWER HERE: "); 
          scanf("%c", choice); 
             
         if(choice[i] == 'b'){
                      
            correct=correct+1;
                  printf("\n------------------------------------------------------------------------------");
                   printf("\n\n ANSWER IS CORRECT");
                  printf("\n------------------------------------------------------------------------------");
                   }
         else{
           wrong=wrong+1;
              printf("\n------------------------------------------------------------------------------");
              printf("\n                             WRONG!!!");
              printf("\n------------------------------------------------------------------------------\n");
              }
         
              
            printf("\n\n\n\n\n\n\n\npress any 'ENTER' for next Questions: ");
             while(!getchar());          
             system("cls"); 
           
           return 0;
           
           
           
           }
    
    
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Ah yes, scanf. Why teachers insist on teaching this CRAP way of entering data is beyond me. Presumably because it sort of mirrors printf.
    Use fgets() to read into a buffer from stdin and parse the resulting string (use sscanf if you want).
    What happens is that you enter "b ENTER", presumably.
    The scanf picks up the b and ignores the ENTER, although bizarrely it doesn't pick up the b UNTIL you press ENTER.
    Assuming you did enter b then it prints "CORRECT".
    Then it prints "press ENTER".
    Since you have done, the next thing it does is clear the screen and exit.

    While you're testing, don't have functions like clear screen. It really helps to confuse you. My guess is that the program is actually working correctly.
     

Share This Page