1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include<iostream>
#include <cctype>
using namespace std;
int check_answers(char student_answers[], const char correct_answers[]); // fixed prototype
const int SIZE = 10; // array size
int main()
{
const char correct_answers[SIZE] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D' }; //changed () to {}, added ''
char student_answers[SIZE]; //array for users answers
char i, temp;
int numCorrect;
for (i = 0; i < SIZE; i++) //changed to 10
{
cout << "Answers to questions are: A, B, C, D." << endl;
cout << "Please enter you answer.\n\n"
<< "A\n"
<< "B\n"
<< "C\n"
<< "D\n\n";
cin >> temp;
temp = toupper(temp);
while (temp != 'A' && temp != 'B' && temp != 'C' && temp != 'D') // check for valid entry
{
cout << "In valid answer" << endl;
cout << "Answers to questions are: A, B, C, D." << endl;
cout << "Please enter you answer.\n\n"
<< "A\n"
<< "B\n"
<< "C\n"
<< "D\n\n";
cin >> temp;
temp = toupper(temp);
}
student_answers[i] = { temp }; // input student answer in the array
}
numCorrect = check_answers(student_answers, correct_answers); //implentation of function to compare arrays.
if (numCorrect >= 8)// if they got 8 or more right
cout << "Congratulations you pass!" << endl;
else
cout << "Sorry you have not passed the exam." << endl;
//need a code to end program if student enters invalid answer 3 times.
return 0;
}
int check_answers(char student_answers[], const char correct_answers[]) // check answer function
{
int numCorrect = 0, i;
for (i = 0; i < SIZE; i++)//move through arrays
{
if (student_answers[i] == correct_answers[i])//campare answers
{
numCorrect++;//if they were the same
}
}
return numCorrect;
}
|