Help with an array comparison program

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;
}



#include <iostream>
using namespace std;


char * getStudentAns(int);
bool * gradeAns (char [], char [], int);
void printResult(bool [], int);
int main()
{
const int QUESTION_NUM = 10;
char stuAns[QUESTION_NUM];
bool correct[QUESTION_NUM];
char correct_ans [QUESTION_NUM] = { 'A', 'A','D','B','C','C','B','D','A','C'};
getStudentAns(QUESTION_NUM);
gradeAns(&correct_ans[QUESTION_NUM], &stuAns[QUESTION_NUM], QUESTION_NUM);
printResult(&correct[QUESTION_NUM], QUESTION_NUM);
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;
}
}

and the output I'm getting looks like this:

Please input your answers
1:A
2:A
3:D
4:B
5:C
6:C
7:B
8:D
9:A
10:C

Correct Answers = -1663236508
Incorrect Answers = 1663236518

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
Last edited on
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?
Nevermind I have figured out the program. Here it is fixed and working

//
// Lab3.cpp
//
//
// Created by Jayme Woodfill on 2/7/18.
//

#include <iostream>
using namespace std;


char * getStudentAns(int);
bool * gradeAns (char [], char [], int);
void printResult(bool [], int);
int main()
{
const int QUESTION_NUM = 10;
char correct_ans [QUESTION_NUM] = { 'A', 'A','D','B','C','C','B','D','A','C'};
char *stuAns = nullptr;
bool *correct = nullptr;

stuAns = getStudentAns(QUESTION_NUM);
correct = gradeAns(correct_ans, stuAns, QUESTION_NUM);
printResult(correct, QUESTION_NUM);
return 0;
}

char * getStudentAns(int QUESTION_NUM)
{
char *stuAnswers = nullptr;
stuAnswers = new char[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 = 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;
}
}
Topic archived. No new replies allowed.