Thread: quiz program

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    quiz program

    What's wrong with this code? When I run it, it first prints the first question, I enter a character (the answer - a or b or c or d), then prints the second question and immediately prints the third question. I can't enter the answer for the second question as well as for the fourth. The code literally jumps over those statements and I don't get why. I get no errors but this works wrong.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    
    int points(char,char,char,char,char);
    
    
    char c1='c', c2='b', c3='b', c4='d', c5='a';
    
    
    int main() {
        char a1, a2, a3, a4, a5; int count;
    
    
        printf("Q1 :Which of the following is a greenhouse gas that is ");
        printf("released by human activities and speeds up global warming?\n");
        printf("a: petroleum\nb: natural gas\nc: carbon dioxide\nd: nuclear power\n\n"); // carbon dioxide
        printf("Your answer: \n\n"); scanf_s("%c",&a1);
    
    
        printf("Q2: Which of the following human activities does NOT release carbon dioxide into the atmosphere?\n");
        printf("a: burning fossil fuels\nb: fishing\nc: deforestation\nd: driving\n\n"); // fishing
        printf("Your answer: \n\n"); scanf_s("%c",&a2);
    
    
        printf("As global warming continues, the intensity of what type of ");
        printf("storm that hits coastlines is predicted to increase?");
        printf("a: tornadoes\nb: hurricanes\nc: tsunamis\nd: earthquakes\n\n"); // hurricanes
        printf("Your answer: \n\n"); scanf_s("%c",&a3);
    
    
        printf("Q4: Rising water temperatures is a result of global warming and may eventually ");
        printf("increase sea levels due to the dissolving of what?\n");
        printf("a: mountains\nb: wetlands\nc: river beds\nd: glaciers\n\n"); // glaciers
        printf("Your answer: \n\n"); scanf_s("%c",&a4);
    
    
        printf("What country emits the most carbon dioxide?\n");
        printf("a: China\nb: United States\nc: Russia\nd: India\n\n"); // China
        printf("Your answer: \n\n"); scanf_s("%c",&a5);
    
    
        count=points(a1,a2,a3,a4,a5);
        printf("YOUR SCORE: %d out of 5\n", count);
        switch(count) {
            case 5: printf("Excellent\n");
            case 4: printf("Very good\n");
            case 3: printf("Time to brush up on your knowledge of global warming.\n");
        }
        _getch();
        return 0;
    }
    
    
    int points(char a1,char a2,char a3,char a4,char a5) {
        int count=0;
        if (a1==c1) {count++;}
        if (a2==c2) {count++;}
        if (a3==c3) {count++;}
        if (a4==c4) {count++;}
        if (a5==c5) {count++;}
        return count;
    }
    Last edited by lmanukyan; 12-22-2015 at 03:54 AM. Reason: typo

  2. #2
    Registered User
    Join Date
    Oct 2015
    Posts
    43
    That's because of \n character being stored in the buffer when you press enter.
    I'll explain better.
    When you do:
    Code:
    printf("Your answer: \n\n"); scanf_s("%c",&a1);
    You enter a character followed by enter. The character is being stored in a piece of memory and a1 points to that memory address. But the \n character is being kept in the buffer.

    Then, in this instruction:
    Code:
    printf("Your answer: \n\n"); scanf_s("%c",&a2);
    It keeps the value stored in the buffer (which is \n) and assign it to a2.
    Then, for a3 the buffer is empty and so you enter the char. As for a4 it keeps \n again and so on....

    You can either use %s which skips spaces; this way:
    Code:
    printf("Your answer: \n\n"); scanf_s("%s",&a1);
    or you can clear the buffer before each input, like this:
    Code:
    while ( getchar()!='\n' );
    printf("Your answer: \n\n"); scanf_s("%c",&a1);
    This way, it skips all chars not equal to '\n' until the first '\n' is encountered.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    3
    An easy way to avoid this happen is to change this:
    Code:
    scanf_s("%c", &yourchar);
    to this

    Code:
    scanf_s(" %c", &yourchar);

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    I changed the c's to s's and an error occured. After entering the first character, this window appeared

    First-chance exception at 0x59EF06E4 (msvcr110d.dll) in EXERCISES.exe: 0xC0000005: Access violation writing location 0x00BF0000.

    If there is a handler for this exception, the program may be safely continued.


    Then I removed all the '\n'-s from all printf("Your answer: \n\n"); scanf_s("%s",&a1);-s but it still skipped the 2nd and 4th

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Go back to your original code and try what GrPlayer suggested in post #3.

    Additional reading in the FAQ: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

  6. #6
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    GrPlayer, thank you! This worked fine but I don't get how, tbh. And no one noticed the big mistake I made: I didn't write the break; statements after each case

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    3
    Quote Originally Posted by lmanukyan View Post
    GrPlayer, thank you! This worked fine but I don't get how, tbh. And no one noticed the big mistake I made: I didn't write the break; statements after each case
    No worries. Glad it worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop in a quiz program
    By feluxz in forum C Programming
    Replies: 6
    Last Post: 12-02-2014, 02:02 PM
  2. Math Quiz Program Help
    By HystericalFool in forum C Programming
    Replies: 5
    Last Post: 11-17-2014, 12:00 AM
  3. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  4. Hi, Quiz C program
    By Eman in forum C Programming
    Replies: 0
    Last Post: 11-11-2009, 04:12 PM
  5. Serial Port/Quiz Program
    By quizkiwi in forum Networking/Device Communication
    Replies: 3
    Last Post: 10-25-2005, 07:47 PM