Can someone help!!!

Ive been trying to get this code to run for days but it keeps crashing for me. can someone tell me what im doing wrong. its due this Friday and im fed up with it.

here's the question:

12. Driver's License Exam The State Department of Motor Vehicles (DMV) has asked you to write a program that grades the written portion of the driver's license exam, which has 20 multiple choice questions. Here are the correct answers:
1. B
2.D
3. A
4. A
5.C
6. A
7. B
8. A
9.C
10.D
11. B
12.C
13.D
14.A
15. D
16.C
17.C
18. B
19.D
20. A
To do this you should create a TestGrader class. The class will have an answers array of 20 characters, which holds the correct test answers. It will have two public member functions that enable user programs to interact with the class: setKey and grade. The setKey function receives a 20-character string holding the correct answers and copies this information into its answers array. The grade function receives a 20-character array holding the test taker's answers and compares each of their answers to the correct one. An applicant must correctly answer 15 or more of the 20 questions to pass the exam. After ''grading'' the exam, the grade function should create and return to the user a string that includes the following information:

• a message indicating whether the applicant passed or failed the exam
• the number of right answers and the number of wrong answers
• a list of the question numbers for all incorrectly answered questions.

The client program that creates and uses a TestGrader object should first make a single call to setKey, passing it a string containing the 20 correct answers. Once this is done it should allow a test taker's 20 answers to be entered, making sure only answers of A-D are accepted, and store them in a 20-character array. Then it should call the grade function to grade the exam and should display the string the function returns. The program should loop to allow additional tests to be entered and graded until the user indicates a desire to quit.


And here's what i have so far:

#include <iostream>
#include <conio.h>
#include <cctype>

using namespace std;

class TestGrader{



private:
int correctAnswers;
int NUM_QUESTIONS;





public:
TestGrader(){

int NUM_QUESTIONS = 20;
}
void setKey(char stu_answers1[]){

const char answers[NUM_QUESTIONS] = {
'B', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D',
'C', 'C', 'B', 'D', 'A'
};

int correctAnswers = 0;
for (int i = 0; i < 20; i++){
if (answers[i] == stu_answers1[i])
correctAnswers++;
}

}
int grade(){


return correctAnswers;
}

};








int main(){
char answer;
do{




const int NUM_QUESTIONS = 20;

char answers[NUM_QUESTIONS] = {
'B', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D',
'C', 'C', 'B', 'D', 'A'
};


TestGrader testObj;
char stu_answers[NUM_QUESTIONS];

//Loop for users answers
for (int replies = 0; replies < NUM_QUESTIONS; replies++) {
cout<< "Please enter your answers (Hint: Use capital letters): " << (replies + 1) << ": ";
cin >> stu_answers[replies];

//Validation of users answers
while (stu_answers[replies] != 'A' && stu_answers[replies] != 'B' && stu_answers[replies] != 'C' && stu_answers[replies] != 'D') {
cout << "You must enter A, B, C, or D\n";

cout<< "Please enter your answers (Hint: Use capital letters): " << (replies + 1) << ": ";
cin >> stu_answers[replies];
}
}

testObj.setKey(stu_answers);


/* int correctAnswers = 0;
for (int i = 0; i < NUM_QUESTIONS; i++) {
if (answers[i] == stu_answers[i])
correctAnswers++;
}
*/
if (testObj.grade() > 20){

}else{
cout << "The list below shows the question numbers of the incorrectly";
cout << " answered questions.\n";
for (int i = 0; i < NUM_QUESTIONS; i++) {
if (answers[i] != stu_answers[i])
cout << "Question # " << i << " is incorrect." << endl;
}
}

//Display the number of correct and incorrect answers provided by the student.
cout << "\nCorrect Answers = " << answers << endl;
cout << "Incorrect Answers = " << NUM_QUESTIONS - testObj.grade() << endl;

if (testObj.grade() >= 15){
cout << "\n\nYou passed the exam!!!\n";
}else{
cout << "\n\nYou failed the exam!!!\n";
}


cout << "Do you want to try again? (Y/N)";
cin >> answer;

if (answer == 'N'){
return 0;
}
}
while(answer == 'Y');


return 0;

}
1
2
3
4
5
6
7
8
9
10
11
int correctAnswers = 0;  //THIS IS LOCAL NOT THE CLASS MEMBER
for (int i = 0; i < 20; i++){
if (answers[i] == stu_answers1[i])
correctAnswers++;
}

}
int grade(){


return correctAnswers;  //THIS IS THE CLASS MEMBER 

you might want a constructor to set correctanswers to 0.

Last edited on
const char answers[NUM_QUESTIONS] = {

this can cause problems. Array sizes should be compile time constants, not variables.

consider making this hard coded to 20 until you get it working, and once you have it working, consider changing it to a pointer (delete in the destructor, etc).

edit.... you do have a constant for it. You also have a variable of the same name. Having stuff with the same name is confusing. Try to avoid that unless you have a good reason to do it, as its easy to confuse scope and introduce bugs. This is the root cause of the above bug, also.


Last edited on
ok its running now but im not getting the correct output. im getting a bunch of random numbers.


#include <iostream>
#include <conio.h>
#include <cctype>

using namespace std;

class TestGrader{



private:
int correctAnswers;
int NUM_QUESTIONS;





public:
TestGrader(){
int correctAnswers = 0;
int NUM_QUESTIONS = 20;
}
void setKey(char stu_answers1[]){

const char answers[20] = {
'B', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D',
'C', 'C', 'B', 'D', 'A'
};

int correctAnswers = 0;
for (int i = 0; i < 20; i++){
if (answers[i] == stu_answers1[i])
correctAnswers++;
}

}
int grade(){


return correctAnswers;
}

};








int main(){
char answer;
do{




const int NUM_QUESTIONS = 20;

char answers[NUM_QUESTIONS] = {
'B', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D',
'C', 'C', 'B', 'D', 'A'
};


TestGrader testObj;
char stu_answers[NUM_QUESTIONS];

//Loop for users answers
for (int replies = 0; replies < NUM_QUESTIONS; replies++) {
cout<< "Please enter your answers (Hint: Use capital letters): " << (replies + 1) << ": ";
cin >> stu_answers[replies];

//Validation of users answers
while (stu_answers[replies] != 'A' && stu_answers[replies] != 'B' && stu_answers[replies] != 'C' && stu_answers[replies] != 'D') {
cout << "You must enter A, B, C, or D\n";

cout<< "Please enter your answers (Hint: Use capital letters): " << (replies + 1) << ": ";
cin >> stu_answers[replies];
}
}

testObj.setKey(stu_answers);


/* int correctAnswers = 0;
for (int i = 0; i < NUM_QUESTIONS; i++) {
if (answers[i] == stu_answers[i])
correctAnswers++;
}
*/
if (testObj.grade() > 20){

}else{
cout << "The list below shows the question numbers of the incorrectly";
cout << " answered questions.\n";
for (int i = 0; i < NUM_QUESTIONS; i++) {
if (answers[i] != stu_answers[i])
cout << "Question # " << i << " is incorrect." << endl;
}
}

//Display the number of correct and incorrect answers provided by the student.
cout << "\nCorrect Answers = " << answers << endl;
cout << "Incorrect Answers = " << NUM_QUESTIONS - testObj.grade() << endl;

if (testObj.grade() >= 15){
cout << "\n\nYou passed the exam!!!\n";
}else{
cout << "\n\nYou failed the exam!!!\n";
}


cout << "Do you want to try again? (Y/N)";
cin >> answer;

if (answer == 'N'){
return 0;
}
}
while(answer == 'Y');


return 0;

}
Topic archived. No new replies allowed.