I came across this True False program but am having difficulty running it. I was trying understand how it is pieced together. Every entry that gets input is marked as correct and it does not read the "text.file" which resides with the cpp. Can someone assist or run it on your end perhaps? Below is the example I found here on cplusplus which I am trying to test out.
The history teacher at your school needs help in grading a true/false test. The students' IDs and test answers are stored in a text file. The first entry in the file contains answers to the test in the form:
TFFTFFTTTFFTFTFTFTT
Every other entry in the file is the studentID, followed by a blank, followed by the student's responses. For example, the entry:
ABC54301 TFTFTFTT TFTFTFFTTFT
Indicated that the student ID is ABC 54301 and the answer to question 1 is true, the answer to question 2 is false, and so on. This student did not answer question 9. The exam has 20 questions, and the class has more than 150 students. Each correct answer is awarded two points, each wrong answer gets one point deducted and no answer gets zero points. Write a program that processes the test data. The output should be the student's ID, followed by the answers, followed by the test score, followed by the test grade. Assume the standard grade scale (90-100 -- a) (80-89 -- b) etc..etc..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main(){
char* data = "text.txt"; //use to open a text file
char student_info[50]; //holds student ID
char student_answers[20]; //stores students' answers
char answers[20]; //holds correct answers and used to compare with students' answers
double score = 0; //counts the students' score
const double max_score = 40; //sets the max score possible and used for calculations
char grade; //stores a letter grade used for output
ifstream in(data); //text.txt must be present in same folder as this ccp file or program will not run correctly or crash.
cout <<"Enter TF answers then press enter:";
cin >> answers;
cout << "Processing student Data;" << " " << answers << " = Correct answers.\n";
while (in >> student_info)
{
in.get(text.txt);
in.getline(student_answers, 21);
for (int i = 0; i < 20; i++)
{
if (student_answers[i] == answers[i])
score += 2;
else if (student_answers[i] == ' ');
else--score;
}
double grade_scale = score / max_score * 100;
if (grade_scale > 89)
grade = 'A';
else if (grade_scale > 79 && grade_scale < 90)
grade = 'B';
else if (grade_scale > 69 && grade_scale < 80)
grade = 'C';
else if (grade_scale > 59 && grade_scale < 70)
grade = 'D';
else if (grade_scale > -40 && grade_scale < 60)
grade = 'F';
cout << endl << student_info << "\t" << student_answers << "\t" << score << " " << grade_scale << "% " << grade << endl;
score = 0;
}
char p; // exit program
cout << "\nDone. Press enter key to quit.";
cin.get(p);
return 0;
}
|