xyrena 0 Newbie Poster

hello, I am trying to learn c++, and I am trying to run this program to display the correct answers in the end. the user will be asked to input their answers to the 20 questions on the list.
for what I have read, I think I should be using some if or nested if statements? please help, thank you. Also, this is not my assignment (as I was accused of from other forum I tried), I am already working and just trying to learn programming on my spare time. thanks again, code is below.

#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("Thankyou for answering the following question");
p("\nCalculating correct answers. Please wait .......");
delay(5000);
p("\nYout total correct answer is : ");
p("\n\t\tEnd of the Program");

getche();
}

Dani AI

Generated

In 's post the main problems are type and input handling rather than logic. Names are declared as single char variables while scanf("%s",...) expects a string pointer; answers are kept in 20 separate variables (A1..A20) which is hard to manage; and "%s" is used where a single-letter answer should use a single-character read. Instead of nested if chains, a single loop that compares each response to an answer key is simpler and far easier to maintain.

Recommended steps:

  • Keep the correct answers in a single array (or std::string) of length 20.
  • Read each response as a single non-whitespace character (e.g. scanf(" %c",&c) or std::cin >> c) and normalize with toupper.
  • Compare c to the key at the same index and increment a running score.
  • Use fixed-size buffers (char name[30]) or std::string/getline for names to avoid overflow.
  • Prefer standard headers over conio.h for portability (Turbo C specifics can remain for legacy use).

Example (concise C++ pattern):

#include <iostream>
#include <string>
#include <cctype>

int main() {
    std::string last, first;
    std::getline(std::cin, last);
    std::getline(std::cin, first);

    const std::string key = "ABCDABCDABCDABCDABCD"; // 20 letters
    int score = 0;
    for (int i = 0; i < 20; ++i) {
        char c;
        std::cout << (i+1) << ") Answer: ";
        std::cin >> c;
        c = std::toupper(static_cast<unsigned char>(c));
        if (c == key[i]) ++score;
    }
    std::cout << "Total correct: " << score << '\n';
}

Notes and cautions: use scanf(" %c", &ch) (leading space skips leftover newlines), add width limits for scanf("%29s", buf), and cast to unsigned char before toupper to avoid undefined behavior. This array+loop pattern scales cleanly and avoids dozens of manual comparisons.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.