Question
I am getting an error here, what am i doing wrong? #include #include using namespace std; int grade(char correctAnswers[], char studentAnswers[], int size); // start
I am getting an error here, what am i doing wrong?
#include
#include
using namespace std;
int grade(char correctAnswers[], char studentAnswers[], int size);
// start main
int main()
{
// constant declaration
const int maxQuestions = 10;
// arrays declaration
char correctAnswers[maxQuestions] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D' };
char studentAnswers[maxQuestions];
// variables declaration
int correctCount = 0, invalidCount = 0;
char answer;
bool passed;
for (int i = 0; i < maxQuestions; i++)
{
// prompt the user to enter the answer
cout << "Enter the answer for the question #" << (i + 1) << ": ";
cin >> answer;
// verify whether the student answer is valid
while (answer != 'A' && answer != 'B' && answer != 'C' && answer != 'D')
{
// count the invalid answers
invalidCount++;
// exit from the program if the invalid answers > 3
if (invalidCount > 3)
{
cout << "GOOD BYE" << endl;
exit(1);
}
// prompt the user to enter valid answer
cout << "Please enter A, B, C, or D only: ";
cin >> answer;
}
// store the answer in the studentAnswers array
studentAnswers[i] = answer;
}
// call the grade function to get the student correct answers
correctCount = grade(correctAnswers, studentAnswers, maxQuestions);
// verify whether the student has passed the exam
if (correctCount >= 8)
{
// if student passed
cout << " Congratulations!" << endl;
cout << "You have passed exam." << endl;
cout << "Total number of correct answers: " << correctCount << endl;
cout << "Total number of incorrect answers: " << (maxQuestions - correctCount) << endl;
}
else
{
// if student failed
cout << " Sorry, you have not passed the exam!" << endl;
cout << "Total number of correct answers: " << correctCount << endl;
cout << "Total number of incorrect answers: " << (maxQuestions - correctCount) << endl;
}
cout << endl;
return 0;
} // end of main function
// grade function implementation
int grade(char correctAnswers[], char studentAnswers[], int size)
{
int correctCount = 0;
for (int i = 0; i < size; i++)
{
if (correctAnswers[i] == studentAnswers[i])
{
correctCount++;
}
}
return correctCount;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started