Hello, I can't figure out one thing about this program! I need your help, please:
One of your professors has asked you to write a program to grade her final exams, which consist of only 20 multiple-choice questions. Each question has one of four possible answers: A, B, C, or D. The file CorrectAnswers.txt contains the correct answers for all of the questions, with each answer written on a separate line. The first line contains the answer to the first question, the second line contains the answer to the second question, and so forth. Write a program that reads the contents of the CorrectAnswers.txt file into a char array, and then reads the contents of another file, containing a student’s answers, into a second char array.
The program should determine the number of questions that the student missed, and then display the following:
• A list of the questions missed by the student, showing the correct answer and the incorrect answer provided by the student for each missed question
• The total number of questions missed
• The percentage of questions answered correctly. This can be calculated as Correctly Answered Questions ÷ Total Number of Questions
• If the percentage of correctly answered questions is 70% or greater, the program should indicate that the student passed the exam. Otherwise, it should indicate that the student failed the exam.
I have this so far, but I don't know how to use the string thingy so that it shows me the line in the file so I can say which questions they missed
int main()
{
const int size = 300;
static int count = 0;
string correctAnswers[size];
string studentAnswers[size];
ifstream infileC;
infileC.open("CorrectAnswers.txt");
if (infileC)
{
for (int i = 0; i<20; i++)
{
infileC >> correctAnswers[i];
}
}
else
{
cout << "File open failure!" << endl;
}
CorrectAnswers.txt looks like this:
It has lines though next to them indicating where they are, I just don't know how to put that here
A
D
B
C
A
A
D
B
D
C
A
D
B
A
C
C
D
B
C
A
StudentAnswers.txt has 220 lines but I am only checking the first 20 lines, which counts as one student's exam, so:
A
D
B
C
B
A
C
B
D
C
A
D
B
A
C
A
D
B
C
A
And I want to say that some of the questions/lines were missed as you can see by the correctanswers.txt
OP: I have good news and bad news for you. On the positive side I can offer you a Student struct with various data members corresponding to your requirements and, additionally, the student name which I think is important, an overloaded << operator for printing and a tried and tested method of extracting the data from file and saving it in standard containers. The bad news is that what I thought would be the easy part, checking the answers, doesn't seem to work. I'm just not being able to reach inside the vector where the student answers are stored and tick them off against the correct answers. I'm posting what I have so far in the hope that some kind soul may be able to help both of us out of our respective miseries.
OP: I'm glad to report that the quirks in the program have been removed. Turned out that the correct_answers vector needed its own iterator to reach inside it, trying
to read it off an index linked to the struct's vector member was not working.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
constint size = 300;
constint EXAMSIZE = 20; // <===== Number of questions
int count;
char correctAnswers[EXAMSIZE]; // <===== Your teacher asked for character arrays
char studentAnswers[EXAMSIZE];
int studentNumber = 0;
// Read correct answers
ifstream infileC( "CorrectAnswers.txt" ); // <===== Open with constructor
if ( !infileC )
{
cout << "Error opening correct answers file" << endl;
return 1; // <===== Stop at failure
}
for ( int i = 0; i < EXAMSIZE; i++ )
{
infileC >> correctAnswers[i];
infileC.ignore( 256, '\n' ); // <===== Make sure to get beyond the linefeed
}
infileC.close();
ifstream infileS( "StudentAnswers.txt" ); // <===== Open with constructor
if ( !infileS )
{
cout << "Error opening student answers file" << endl;
return 1; // <===== Stop at failure
}
// Student by student
while ( infileS ) // <===== Comment out this line for only one student
{
studentNumber++; // <===== Next student
// Read answers
for ( int i = 0; i < EXAMSIZE; i++ )
{
infileS >> studentAnswers[i];
if ( !infileS ) return 0; // <===== Need opportunity to escape if no more students (don't we all!)
infileS.ignore( 256, '\n' );
}
// Produce report
cout << "Report for Student " << studentNumber << ": " << endl;
cout << "The student got the following answers wrong: " << endl;
count = 0;
for ( int i = 0; i < EXAMSIZE; i++)
{
if ( correctAnswers[i] != studentAnswers[i] )
{
count++;
cout << "Question number: " << setw( 2 ) << i + 1
<< " Student answer: " << studentAnswers[i]
<< " Correct answer: " << correctAnswers[i] << endl;
}
}
cout << endl;
double percent = 100.0 * ( EXAMSIZE - count ) / EXAMSIZE; // <===== 100.0 first prevents integer division error
cout << "Number of questions wrong: " << count << endl;
cout << "Percentage correct: " << percent << endl;
cout << endl;
if ( percent >= 70.0 ) cout << "This student passed the exam" << endl;
else cout << "This student failed the exam" << endl;
cout << endl << endl;
} // <=====Comment out this line for only one student
infileS.close();
}
studentAnswers.txt:
A
D
B
C
B
A
C
B
D
C
A
D
B
A
C
A
D
B
C
A
A
D
D
C
B
A
C
B
D
A
A
D
B
A
C
B
C
B
E
A
Output:
Report for Student 1:
The student got the following answers wrong:
Question number: 5 Student answer: B Correct answer: A
Question number: 7 Student answer: C Correct answer: D
Question number: 16 Student answer: A Correct answer: C
Number of questions wrong: 3
Percentage correct: 85
This student passed the exam
Report for Student 2:
The student got the following answers wrong:
Question number: 3 Student answer: D Correct answer: B
Question number: 5 Student answer: B Correct answer: A
Question number: 7 Student answer: C Correct answer: D
Question number: 10 Student answer: A Correct answer: C
Question number: 16 Student answer: B Correct answer: C
Question number: 17 Student answer: C Correct answer: D
Question number: 19 Student answer: E Correct answer: C
Number of questions wrong: 7
Percentage correct: 65
This student failed the exam