Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   C- if problem (http://www.go4expert.com/forums/c-if-problem-t14661/)

visokeeg 21Oct2008 10:44

C- if problem
 
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;
        }


xpi0t0s 21Oct2008 21:53

Re: C- if problem
 
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.

visokeeg 22Oct2008 07:03

Re: C- if problem
 
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;
     
     
     
      }


xpi0t0s 22Oct2008 14:19

Re: C- if problem
 
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.


All times are GMT +5.5. The time now is 07:39.