True or False Quiz (Need help)

This is a discussion on True or False Quiz (Need help) within the C Programming forums, part of the General Programming Boards category; #include <stdio.h> #include <ctype.h> #define TRUE 1 #define FALSE 0 typedef struct question_type{ char question[200]; int response;} question_type; const struct ...

  1. #1
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43

    True or False Quiz (Need help)

    #include <stdio.h>
    #include <ctype.h>

    #define TRUE 1
    #define FALSE 0

    typedef struct question_type{
    char question[200];
    int response;}
    question_type;

    const struct question_type question_table[4] =
    {
    { "Are you happy?", TRUE },
    { "Are you sure?", TRUE },
    { "Did World War 1 end in 1942?", FALSE },
    { "Did World War 2 end in 1942?", TRUE }
    };

    int main(void)
    {
    int cnt, score;
    int num_questions = 4;
    char response;
    score = 0;
    cnt = 1;

    while (cnt < num_questions)
    {
    printf(question_table[cnt].question);
    printf("(Y/N)\n");
    cnt++;
    scanf("%c", &response);
    response = toupper(response);
    if ((response == 'Y' && question_table[cnt].response == TRUE)
    || (response == 'N' && question_table[cnt].response == FALSE))
    {
    printf("Correct\n");
    score++;
    response = 0;
    }else{
    printf("Wrong answer.\n");
    response = 0;
    }
    }
    printf("You got %d out of %d questions (%d %%)\n", score, num_questions,
    100*score/num_questions);
    return 0;
    }

    That is what I have so far. When you compile it, and enter the answer it doesn't recongize Y or N, then it will jump 2 questions and go to the last one. I've messed around with it now for a week and haven't been able to come up with an answer. If you could tell me what i'm doing wrong and what line to make changes I would greatly appretiate it. Also ignore the questions in there, they are just for now.

    --Twigg

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    Add this line as shown
    Code:
    scanf("%c", &response);
    while ( getchar() != '\n' );  // add this
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    I think you need to clear the buffer for the keyboard

    i.e

    fflush(stdin);

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    > fflush(stdin);
    Right idea - wrong way of doing it
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43
    Thanks, that stopped the skipping of questions. Now the problem is when I enter y for the first question, it says its correct like its supposed to. Then when I enter the right answer for the next questions it says its wrong. Maybe I need to reset response to something?


    --Twigg

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    > cnt = 1;
    Are you sure?
    You seem to be starting with the 2nd question.

    > scanf("%c", &response);
    Try following it with this line
    printf( "response=%d\n", response );

    This will print the decimal value of the char you read.

    If it isn't one of
    N=78 Y=89 n=110 y=121
    you know where to start looking
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Ian
    Ian is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    minor note

    uhh.. cnt should start at 0 not 1
    because you're skipping the first question when it starts at 1

    anyway, a minor note (:

    Ian

  8. #8
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43
    Well i've checked out the input/output part, and its sending it correctly. I'm really not sure why its not working. Heres an updated version of the changes i made. Anything after the first question, the answer is opposite of what it should be.



    #include <stdio.h>
    #include <ctype.h>
    #define TRUE 1
    #define FALSE 0



    typedef struct question_type
    {
    char question[200];
    int response;
    }
    question_type;


    const struct question_type question_table[10] =
    {
    { "Are you happy?", TRUE },
    { "Are you sure?", TRUE },
    { "Did World War 1 end in 1942?", FALSE },
    { "Did World War 2 end in 1942?", TRUE }
    };

    int main(void)
    {
    int cnt, score;
    int num_questions = 4;
    char response;
    score = 0;
    cnt = 0;

    while (cnt < 4)
    {
    printf(question_table[cnt].question);
    printf("(Y/N)\n");
    cnt++;
    scanf("%c", &response);
    while ( getchar() != '\n' );
    response = toupper(response);
    if ((response == 'Y' && question_table[cnt].response == TRUE)
    || (response == 'N' && question_table[cnt].response == FALSE))
    {
    printf("Correct\n");
    score++;
    }else{
    printf("Wrong answer.\n");
    }
    }
    printf("You got %d out of %d questions (%d %%)\n", score, num_questions,
    100*score/num_questions);
    return 0;
    }




  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    1) WW2 ended in 1942??? Funny, as apparently the US fought for 3 more years for no reason, then.

    Your problem is that you increment cnt partway through the loop, so it reads the question, increments, and compares the answer to the NEXT question's answer.

    So, move cnt++ to the end of the while loop, and all will be good.

    To prevent this, you should probably just make the while loop a FOR loop --

    for (cnt = 0; cnt < 4; cnt++)

    and remove the cnt++ from the loop.

  10. #10
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by task
    I think you need to clear the buffer for the keyboard

    i.e

    fflush(stdin);
    fflush'ing an input stream is not allowed .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  2. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 01:48 PM
  3. 1 or 0 == true or false?
    By Discolemonade in forum C++ Programming
    Replies: 4
    Last Post: 08-14-2005, 05:08 PM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21