Hi guys. I am currently making a Question and Answer multiple choice Program. So far it is good. I used Array to shuffle 50 questions. the problem of the program is it cannot count the right answers and display it. This is the part of the program that has an error.

cout<<x[list[0]]<<endl;
cin>>a;
if (a==y[list[0]])
i++;
cout<<"Total= "<<i<<"\n"<<endl;

Can you please tell me what is wrong with it? If you want to see my whole program just ask me and i hope you can help me.

Dani AI

Generated

Common causes for a correct-answer counter that never changes are usually simple and repeated across similar programs: the counter variable is not initialized or is shadowed inside the loop; only the first shuffled index is tested instead of iterating the whole list; the “Total” is printed inside the question loop (so it looks wrong); input / expected-value types mismatch (char vs string or case differences); or the questions and answers become desynchronized when shuffling. As noted, any shuffle must preserve the mapping between a question and its correct answer.

A minimal, robust pattern to follow (keeps the association, does a single final print, and handles case) looks like this:

int total = 0;                       // initialize once
for (int q = 0; q < numQuestions; ++q) {
    int idx = list[q];               // index into question/answer arrays
    cout << questions[idx] << endl;
    char ans;
    cin >> ans;
    ans = toupper(static_cast<unsigned char>(ans));
    char expected = toupper(static_cast<unsigned char>(correctAnswers[idx]));
    if (ans == expected) ++total;
}
// print once, after checking every question
cout << "Total = " << total << endl;

Practical debugging checklist: confirm total (or i) is declared and set to 0 before the loop and not redeclared inside the loop; verify the loop runs for the intended number of questions (use list[q], not always list[0]); print idx and correctAnswers[idx] while testing to confirm the mapping; ensure input handling tolerates extra whitespace and case; and when shuffling either swap both parallel arrays together or use a single struct/record for question+answer and shuffle that so the pair stays intact.

Tying back to earlier replies: ’s request for more context is sensible — a full snippet would expose scoping/type issues — and ’s shuffle warning is likely the key if the counter logic and printing are otherwise correct.

Recommended Answers

All 4 Replies

Yes, need more context. Post more. And tell us what it is doing that makes you say that it doesn't work.

i cant get ur problem....
just show ur program

when you shuffle the questions do you also shuffle the answers at the same time, that is, when a swap is needed you have to swap elements in both arrays so that the two arrays remain in the same order.

Oh well, I just answered a dead thread :)

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.