I'm trying to write a program that will take an array of correct answers
const char correctAnswers[] = {'B','D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D'};
compare that to a user input of they answers...
char studentAnswers[] = {};
and then find out how many of the answers they got correct, but I find myself running into errors trying to compare the two arrays
bool grade(char correctAnswers, char studentAnswers){
for(int b=0; b<10; b ){
for(int j=0; j<10; j ){
if(correctAnswers[b]==studentAnswers[j]){ //HERE
ansCorrect;
}
}
}
if(ansCorrect >= 8){ //8 out of 10 correct
return true;}
else;
return false;
}
now I don't agree with some of the methods I had to use to make this function but it is required that I have the program return true or false for if they passed. I've been tweaking with it for a bit and my whole program only shows and error on the line where is says //HERE there error message is (Subscripted value is not an array, pointer, or vector)
here is my full code if that would help, be warned it is very messy and there are definitely some unnecessary variables and global variables.
#include <iostream>
using namespace std;
bool grade();
int ansCorrect = 0;
const char correctAnswers[] = {'B','D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D'};
char studentAnswers[] = {};
int main ()
{
int i = 0;
int questionNum = 1;
int chances = 0;
char response;
//Ask for student answer and checks if the input is valid
do {
cout << "What was the answer to Question "<< questionNum<< ": \n";
cin >> response;
switch (response){
case 'A':
studentAnswers[i] = response;
questionNum ;
i ;
break;
case 'a':
studentAnswers[i] = response;
questionNum ;
i ;
break;
case 'B':
studentAnswers[i] = response;
questionNum ;
i ;
break;
case 'b':
studentAnswers[i] = response;
questionNum ;
i ;
break;
case 'C':
studentAnswers[i] = response;
questionNum ;
i ;
break;
case 'c':
studentAnswers[i] = response;
questionNum ;
i ;
break;
case 'D':
studentAnswers[i] = response;
questionNum ;
i ;
break;
case 'd':
studentAnswers[i] = response;
questionNum ;
i ;
break;
default:
cout << "Please enter a valid input (A, B, C, D)\n";
chances ;
if (chances == 3){
cout << "GOODBYE";
break; }
}
} while (i < 10);
bool result = grade();
if (result==true){
cout << "Congratulations!\nYou have passed exam.";
if (result==false)
cout << "Sorry, you have not passed the exam!";
}
cout << "total number of correct answers: "<< ansCorrect;
cout << "total number of incorrect answers: "<< 10 - ansCorrect;
return 0;
}
bool grade(char correctAnswers, char studentAnswers){
for(int b=0; b<10; b ){
for(int j=0; j<10; j ){
if(correctAnswers[b]==studentAnswers[j]){
ansCorrect;
}
}
}
if(ansCorrect >= 8){//8 out of 10 correct
return true;}
else;
return false;
}
I would also like to add that some of the methods I used might not be idle so if you have any opinions on easier methods that would help alot!
CodePudding user response:
Unfortunately, your code contains multiple errors.
char studentAnswers[] = {};
Here you create an empty array (what obviously makes no sense in your application). Seems like you want it to have 10 elements:char studentAnswers[10] = {};
.- The
grade(...)
function you implemented accepts twochar
as parameters. You cannot use indexing on them. Instead, you want to accept arrays (exactly, pointers to arrays):grade(char const* correct, char const* student)
. - Finally, as paddy suggested, you are calculating the number of correct answers wrongly. Check out their comment.
Glad to hear if I missed something.
CodePudding user response:
The parameters in the function grade need to be declared as an array. Also declare the size of the empty array
const char correctAnswers[] = {'B','D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D'};
char studentAnswers[sizeof correctAnswers] = {};
bool grade(char correctAnswers[], char studentAnswers[]){
for(int b=0; b<correctAnswers.size(); b ){
for(int j=0; j<studentAnswers.size(); j ){
if(correctAnswers[b]==studentAnswers[j]){
ansCorrect;
}
}
}
return ansCorrect >= 8; //8 out of 10 correct
}