C- if problem

visokeeg's Avatar, Join Date: Oct 2008
Newbie Member
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;
        }
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
visokeeg's Avatar, Join Date: Oct 2008
Newbie Member
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;
       
       
       
       }
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.