I'm writing this program for my computer science class and thought I had it figured out for the most part, but I'm getting questions saying they're incorrect when I enter the correct answers. Please help!!
1. Your program should store the correct answers in an array correct_ans. Your main function should
call getStudentAns function asking users to enter the student’s answers for each of the 10 questions, and
the function returns an array storing the answers. After the student’s answers have been entered, the main
function will call gradeAns function to grade the answers and returns a boolean array where true means
correct and false means wrong. Call printResult function to display a message indicating whether the
student passed or failed the exam. (A student must correctly answer 7 of the 10 questions to pass the
exam.) It should then display the total number of correctly answered questions, the total number of
incorrectly answered questions, and a list showing the question numbers of the incorrectly answered
questions.
? getStudentAns(int);
? gradeAns (char [], char [], int);
void printResult(bool [], int);
int main()
{
char correct_ans [QUESTION_NUM] = { 'A', 'A','D','B','C','C','B','D','A','C'};
......
......
return 0;
}
char * getStudentAns(int QUESTION_NUM)
{
char stuAnswers[QUESTION_NUM];
cout << "Please input your answers" << endl;
for (int i = 0; i < QUESTION_NUM; ++i)
{
cout << (i+1) << ":";
cin >> stuAnswers[i];
};
return stuAnswers;
};
bool * gradeAns (char correct_ans1[], char stuAns1[], int QUESTION_NUM)
{
bool correct[10] = {false};
for (int i = 0; i < QUESTION_NUM; i++)
{
if (correct_ans1[i] == stuAns1[i])
correct[i] = true;
}
return correct;
}
void printResult(bool correct1[], int QUESTION_NUM)
{
int numCorrect;
int passingScore = 7;
for (int i = 0; i < QUESTION_NUM; i++)
{
if(correct1[i] == true)
numCorrect++;
}
//Prints the number of incorrect and correct questions
cout << "\nCorrect Answers = " << numCorrect << endl;
cout << "Incorrect Answers = " << QUESTION_NUM - numCorrect << endl;
//Did they pass or fail?
cout << "\nYou must have at least 7 correct to pass.";
if (numCorrect >= passingScore) {
cout << "\nStudent passed the exam\n\n";
}
else {
cout <<"\nStudent failed the exam\n\n";
}
cout << "The list below shows the question numbers of the incorrectly";
cout << " answered questions.\n";
for (int i = 0; i < QUESTION_NUM; i++)
{
if (correct1[i] != true)
cout << "Question # " << i+1 << " is incorrect." << endl;
}
}
You must have at least 7 correct to pass.
Student failed the exam
The list below shows the question numbers of the incorrectly answered questions.
Question # 1 is incorrect.
Question # 2 is incorrect.
Question # 3 is incorrect.
Question # 7 is incorrect.
Question # 9 is incorrect.
Question # 10 is incorrect.
Program ended with exit code: 0
Your code is very messy. If you don't know how to use pointers, avoid using them at all as they will lead to trouble. Your pointer returning functions don't really utilize the return type at all, so it's best to just make them void.
Take a look at your functions. Some of them should have been passed parameter(s) from main. Can you figure out which ones?
cout << "Please input your answers" << endl;
for (int i = 0; i < QUESTION_NUM; ++i)
{
cout << (i+1) << ":";
cin >> stuAnswers[i];
};
return stuAnswers;
}
bool * gradeAns (char correct_ans1[], char stuAns1[], int QUESTION_NUM)
{
bool *correct = nullptr;
correct = new bool[QUESTION_NUM];
for (int i = 0; i < QUESTION_NUM; i++)
{
bool arraysEqual = true;
if (correct_ans1[i] != stuAns1[i])
{
arraysEqual = false;
}
if (arraysEqual)
correct[i] = true;
else
correct[i] = false;
}
return correct;
}
void printResult(bool correct1[], int QUESTION_NUM)
{
int numCorrect = 0;
int passingScore = 7;
for (int i = 0; i < QUESTION_NUM; i++)
{
if(correct1[i] == true)
numCorrect++;
}
//Prints the number of incorrect and correct questions
cout << "\nCorrect Answers = " << numCorrect << endl;
cout << "Incorrect Answers = " << QUESTION_NUM - numCorrect << endl;
//Did they pass or fail?
cout << "\nYou must have at least 7 correct to pass.";
if (numCorrect >= passingScore) {
cout << "\nStudent passed the exam\n\n";
}
else {
cout <<"\nStudent failed the exam\n\n";
}
cout << "The list below shows the question numbers of the incorrectly";
cout << " answered questions.\n";
for (int i = 0; i < QUESTION_NUM; i++)
{
if (correct1[i] != true)
cout << "Question # " << i+1 << " is incorrect." << endl;
}
}