mardi 1 mars 2016

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!

Aucun commentaire:

Enregistrer un commentaire