Search code examples
c++turbo-c++

How to count the correct answers in this C++ code


I am trying to run this code, where it will count how many correct answers the user made.

But I don't know how. I think I should use the if or nested if's to do that. How can I do it?

This will ask for a user to input their correct answer, A, B, C, or D. Then I want to increment the n:

#include <stdio.h>
#include <conio.h>

#define p printf
#define s scanf

main () {
    int Ttl;
    char Ln, Mi, Fn,
         A1,   A2,  A3,  A4,  A5,  A6,  A7,  A8,  A9, A10,
         A11, A12, A13, A14, A15, A16, A17, A18, A19, A20;

    clrscr();

    p("\n)
    p("\n\nEnter your last name: ");
    s("%s", &Ln);
    p("\nEnter your middle initial: ");
    s("%s", &Mi);
    p("\nEnter your first name: ");
    s("%s", &Fn);
    p("\nType the corresponding letter to each question.");
    p("\n1.) Shortcut for'New Line'");
    p("\n  A. Ctrl + R          C. Ctrl + M ");
    p("\n  B. Ctrl + QC          D. Ctrl + KC ");
    p("\nAnswer: ");
    s("%s", A1);
    p("\n2.) Shortcut for'Up Arrow Key'");
    p("\n  A. Ctrl + Z          C. Ctrl + X ");
    p("\n  B. Ctrl + QC          D. Ctrl + E ");
    p("\nAnswer: ");
    s("%s", A2);
    p("\n3.) Shortcut for'Down Arrow Key'");
    p("\n  A. Ctrl + X          C. Ctrl + E ");
    p("\n  B. Ctrl + C          D. Ctrl + R ");
    p("\nAnswer: ");
    s("%s", A3);
    p("\n4.) Shortcut for'Scroll Down'");
    p("\n  A. Ctrl + X          C. Ctrl + R ");
    p("\n  B. Ctrl + E          D. Ctrl + Z ");
    p("\nAnswer: ");
    s("%s", A4);
    p("\n5.) Shortcut for'Page Up'");
    p("\n  A. Ctrl + K          C. Ctrl + R ");
    p("\n  B. Ctrl + Q          D. Ctrl + C ");
    p("\nAnswer: ");
    s("%s", A5);
    p("\n6.) Shortcut for'Page Down'");
    p("\n  A. Ctrl + Q          C. Ctrl + K ");
    p("\n  B. Ctrl + C          D. Ctrl + Z ");
    p("\nAnswer: ");
    s("%s", A6);
    p("\n7.) Shortcut for'Top of a File'");
    p("\n  A. Ctrl + KY          C. Ctrl + KC  ");
    p("\n  B. Ctrl + QC          D. Ctrl + QR ");
    p("\nAnswer: ");
    s("%s", A7);
    p("\n8.) Shortcut for'Bottom of a File'");
    p("\n  A. Ctrl + QC          C. Ctrl + KY ");
    p("\n  B. Ctrl + QS          D. Ctrl + KC ");
    p("\nAnswer: ");
    s("%s", A8);
    p("\n9.) Shortcut for'Copy Block'");
    p("\n  A. Ctrl + QY          C. Ctrl + KC ");
    p("\n  B. Ctrl + KY          D. Ctrl + QC ");
    p("\nAnswer: ");
    s("%s", A9);
    p("\n10.) Shortcut for'Delete Block'");
    p("\n  A. Ctrl + QR          C. Ctrl + QC ");
    p("\n  B. Ctrl + KC          D. Ctrl + KY ");
    p("\nAnswer: ");
    s("%s", A10);
    p("\n11.) Hot Keys Command 'Saves the file currently being edited'");
    p("\n  A. F6          C. F2 ");
    p("\n  B. F4          D. F9 ");
    p("\nAnswer: ");
    s("%s", A11);
    p("\n12.) Hot Keys Command'Loads a File'");
    p("\n  A. F7          C. F1");
    p("\n  B. F3          D. F8");
    p("\nAnswer: ");
    s("%s", A12);
    p("\n13.) Hot Keys Command'Compiles and link the program'");
    p("\n  A. F4          C. F3 ");
    p("\n  B. F2          D. F9 ");
    p("\nAnswer: ");
    s("%s", A13);
    p("\n14.) Hot Keys Command'Compiles file to .obj file'");
    p("\n  A. Alt + F9          C. Alt + F5 ");
    p("\n  B. Alt + F2          D. Alt + F7 ");
    p("\nAnswer: ");
    s("%s", A14);
    p("\n15.) Hot Keys Command'Next error'");
    p("\n  A. Alt + F4          C. Alt + F9 ");
    p("\n  B. Alt + F7          D. Alt + F8 ");
    p("\nAnswer: ");
    s("%s", A15);
    p("\n16.) Hot Keys Command'Previous error'");
    p("\n  A. Alt + F7          C. Alt + F9 ");
    p("\n  B. Alt + F3          D. Alt + F1 ");
    p("\nAnswer: ");
    s("%s", A16);
    p("\n17.) Hot Keys Command'Activates the file menu'");
    p("\n  A. Alt + E           C. Alt + C ");
    p("\n  B. Alt + F          D. Alt + O ");
    p("\nAnswer: ");
    s("%s", A17);
    p("\n18.) Hot Keys Command'Quit the Turbo C program'");
    p("\n  A. Alt + D          C. Alt + P ");
    p("\n  B. Alt + C          D. Alt + X ");
    p("\nAnswer: ");
    s("%s", A18);
    p("\n19.) Hot Keys Command'Runs the program'");
    p("\n  A. Ctrl + F3          C. Ctrl + F2 ");
    p("\n  B. Ctrl + F5          D. Ctrl + F9 ");
    p("\nAnswer: ");
    s("%s", A19);
    p("\n20.) Hot Keys Command'Switches between windows'");
    p("\n  A. F2          C. F6 ");
    p("\n  B. F4          D. F8 ");
    p("\nAnswer: ");
    s("%s", A20);
    p("Thank you for answering the following question");
    p("\nCalculating correct answers. Please wait...");
    delay(5000);
    p("\nYour total correct answer is: ");
    p("\n\t\tEnd of the Program");

    getche();
}

Solution

  • Here is an approach which uses an array of structures. This way you can keep as many parameters (related to the question) in the same place:

    #include <stdio.h>
    
    
    typedef struct {
        const char *question;
        int correct_answer;
        int given_answer;
    } question_t;
    
    
    int main() {
    
        question_t questions[] = {
            {"how many wings does bird have?\n1) 55\t3) 3\n2) 5\t4) 2", 4},
            {"how many heads does frog have?\n1) 55\t3) 3\n2) 1\t4) 2", 2},
            {NULL},
        };
        
        int number_of_corrent_unswers = 0;
        for(int i=0; questions[i].question != NULL; i++) {
            puts(questions[i].question);
            scanf("%d", &questions[i].given_answer);
            if(questions[i].given_answer == questions[i].correct_answer) {
                number_of_corrent_unswers++;
            }
        }
        printf("number of correct answers: %d\n", number_of_corrent_unswers);
        return 0;
    }