Derek Vaughan Derek Vaughan - 3 years ago 170
C++ Question

Confusion over making a multiple choice test with arrays in C++

For class i am tasked with creating a multiple choice test that has a set of correct answers pre-given. The user is prompted to enter 20 answers, then the program is supposed to compare the correct answers with student's inputted answers. I am confused over rewriting the size of an array, as I am trying to place all the student's correct and incorrect answers into two new arrays, then outputting them for the user to see which he/she got correct.

My code is:

#include<iostream>
#include<string>
using namespace std;

int main()
{
char answers[20] = { 'A','D','B','B','C','B','A','B','C','D','A','C','D','B','D','C','C','A','D','B' };
char studentAnswers[20];
int correctlyAnswered[20];
int falselyAnswered[20];
for (int i = 0; i <= 19; i++)
{
cout << "Please enter your answer for question #" << i+1 << ": ";
char choice;
cin >> choice;
if (choice != 'A' || choice != 'B' || choice != 'C' || choice != 'D')
{
cout << "Error: Answer must be A,B,C or D. Please enter a new answer: ";
cin >> choice;
}
studentAnswers[i] = choice;
cin.sync();
if (answers[i] = studentAnswers[i])
{
studentAnswers[i] = correctlyAnswered[i];
}
else
{
studentAnswers[i] = falselyAnswered[i];
}


}
cout << "You answered " << sizeof(correctlyAnswered[20]) << " questions correctly out of 20" << endl;
system("pause");
return 0;


}

The code isnt entirely finished, though I would like to sort this issue out before proceeding. I believe my issue stems from setting the size of the correctlyAnswered and falselyAnswered arrays to 20, though i'm not sure how else to get around this. Please help!

Answer Source

I suggest to increment an int every time the student answers correctly.

Therefore I would modify the code declaring:

int correctlyAnswered = 0;

Then in the for cycle I would:

if (answers[i] == choice )
  correctlyAnswered++;

(Notice == not =)
Finally you can output:

cout << "You answered " << correctlyAnswered << " questions correctly...

Some other suggestions:
- use a while cycle to get user input

while(1) {
  cin >> choice;
  if (choice != 'A' || choice != 'B' || choice != 'C' || choice != 'D')
    cout << "Error: Answer must be A,B,C or D. Please enter a new answer: ";
  else
    break;
}
  • calculate falselyAnswered:

    falselyAnswered = 20 - correctlyAnswered;

Update In order to store the question the sutdent answer correctly I would declare a bool array:

int correctlyAnswered = 0;
bool correct[20];

Then modify the if:

if (answers[i] == choice ) {
  correctlyAnswered++;
  correct[i] = true;
} else {
  correct[i] = false;
}

Then printout the results:

cout << "You answered " << correctlyAnswered << " questions correctly"<<endl;
for( int i=0; i<20; i++ ) {
   count << "Your answer to question " << i+1 << " is ";
   if ( correct[i] )
      cout << "correct" << endl;
   else 
      cout << "wrong" << endl;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download