In the following program, we are supposed to read in 20 scores from the user, an
ID: 3639907 • Letter: I
Question
In the following program, we are supposed to read in 20 scores from the user, and then check these 20 scores against a data file with the correct answers. After this is done, the program returns the users score (out of 20) and says whether they pass or fail.I have everything done, except I cannot seem to get "numCorrect" to pass into the next function. If I print out "numCorrect" in the "checkScores" function, it returns the right value. However, if I print out "numCorrect" in the next function (passFail), then it just prints a zero. Why is this?
Source code:
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
char scores[20];
char answer;
int numCorrect = 0;
void getScores(char []);
int checkScores(char [], char, int);
void passFail(int);
int main() // Main function
{
cout << "---- Driver's License Exam Grader ---- ";
cout << "Please enter the answers to grade the exam ";
getScores(scores); // Calls getScores function
checkScores(scores, answer, numCorrect);
passFail(numCorrect);
return 0;
}
void getScores(char scores[]) // Function to read the student's scores
{
for (int count = 1; count <= 20; count++) // Read 20 scores from user
{
cout << "Please enter the score of student " << count << ": ";
cin >> scores[count];
while ((scores[count] != 'A' && scores[count] != 'B' && // Validate input
scores[count] != 'C' && scores[count] != 'D'))
{
cout << "Please enter A, B, C or D (upper case): ";
cin >> scores[count];
}
}
}
int checkScores(char scores[], char answer, int score)
// Function to check entered
// scores against correct scores
{
int count = 0;
ifstream inputFile;
inputFile.open("answerkey.dat");
while (count < 20 && inputFile >> answer)
{
if (scores[count] == answer)
score++;
count++;
}
// cout << numCorrect << " ";
inputFile.close();
}
void passFail(int numCorrect)
{
cout << "Your score was " << numCorrect << "/20. ";
if (numCorrect >= 15)
cout << "You passed the test! ";
else
cout << "You failed, please try again ";
}
Explanation / Answer
You need to pass numCorrect by reference.
int checkScores(char [], char, int&);
and
int checkScores(char scores[], char answer, int &score)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.