Intro to c++. The history teacher at your school needs help in grading a True/Fa
ID: 3813138 • Letter: I
Question
Intro to c++. The history teacher at your school needs help in grading a True/False test. The students’ ID numbers and test answers are stored in a file. The first entry in the file contains answers to the test in the form TFFTF Every other entry in the file is the student ID number, followed by a #, followed by the student’s response. For example, the entry 123456#TF FT indicates that the student's ID is 123456 and the answer to question 1 is True, the answer to question 2 is False, the student did not answer question 3 (blank), the answer to question 4 is False, and the answer to question 5 is True. The exam has 5 questions, and the class has 25 students (use const variables to store these). Each correct answer is awarded twenty points, each wrong answer gets –10 point, and no answer gets 0 points. Write a program that processes the test data. The output should print the following. First line should be the solution, i.e. Exam Key. Next, the data should be formatted in a Table to print: the student’s name, followed by the answers, followed by the test score, followed by the test grade. Assume the following grade scale: 90%–100%, A; 80%–89.99%, B; 70%–79.99%, C; 60%–69.99%, D; and 0%–59.99%, F. Use manipulators from to format the output neatly in well-aligned rows and columns and write the data to a file as shown in the sample output. Use the input file called "input_hw6.txt" that is provided. Remember to use file error checking and close files when done. Hint: Use the get() function to read student answers, because you need to read ' ' blank too. Example if you used a ifstream variable called in_file and have a char variable called student_ans to read the student answers, then you can read a single char (letter) from the file using the statement: in_file.get(student_ans);
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream in_file("input_hw6.txt", std::ios_base::in); //opening input file
if (in_file.is_open()) //error checking
cout << "Input File successfully open"<<endl;
else
cout << "Error opening input file"<<endl;
fstream out_file("output_hw6.txt", std::ios_base::out); //opening output file
if (out_file.is_open()) //error checking
cout << "Output File successfully open";
else
cout << "Error opening output file";
string Exam_key;
in_file>>Exam_key; //reading exam key
out_file<<Exam_key<<endl; //printing key to output file
int id;
char student_ans[5],delimit;
while(in_file>>id>>delimit) //reading student id and delimiter #
{
int total_score=0;
//getting student responce
in_file.get(student_ans[0]);
in_file.get(student_ans[1]);
in_file.get(student_ans[2]);
in_file.get(student_ans[3]);
in_file.get(student_ans[4]);
//calculating student score
if(student_ans[0]!=' ')
{
if(student_ans[0]==Exam_key[0])
total_score+=20;
else
total_score-=10;
}
if(student_ans[1]!=' ')
{
if(student_ans[1]==Exam_key[1])
total_score+=20;
else
total_score-=10;
}
if(student_ans[2]!=' ')
{
if(student_ans[2]==Exam_key[2])
total_score+=20;
else
total_score-=10;
}
if(student_ans[3]!=' ')
{
if(student_ans[3]==Exam_key[3])
total_score+=20;
else
total_score-=10;
}
if(student_ans[4]!=' ')
{
if(student_ans[4]==Exam_key[4])
total_score+=20;
else
total_score-=10;
}
//calculating percentage
float percentage=total_score/(float)100*(float)100;
char grade=' ';
//assigning the grades
if(percentage>=90)
grade='A';
else if(percentage>=80&&percentage<90)
grade='B';
else if(percentage>=70&&percentage<80)
grade='C';
else if(percentage>=60&&percentage<70)
grade='D';
else
grade='F';
//output to the file
out_file<<id<<" "<<student_ans[0]<<student_ans[1]<<student_ans[2]<<student_ans[3]<<student_ans[4]<<" Test Score: "<<total_score<<" Percentage:"<<percentage<<" "<<" Grade: "<<grade<<endl;
}
in_file.close();
out_file.close();
return 0;
}
output:-
Input File successfully open
Output File successfully open
Process exited normally.
Press any key to continue . . .
input file:-
tftft
123456#tftft
123457#ftftf
123458#ff tt
123459#tt ff
123460#t fff
123461#f ttt
123462#tftft
123463#ftftf
123464#tftt
123465#fftt
123466#f tt
123467#tf
123468#tftf
123469#f
123470#tt
123471#ff
123472#ffff
123473#t t t
123474#fffff
123475#ftftt
output file:-
tftft
123456 tftft Test Score: 100 Percentage:100 Grade: A
123457 ftftf Test Score: -50 Percentage:-50 Grade: F
123458 ff tt Test Score: 20 Percentage:20 Grade: F
123459 tt ff Test Score: 20 Percentage:20 Grade: F
123460 t fff Test Score: 20 Percentage:20 Grade: F
123461 f ttt Test Score: 20 Percentage:20 Grade: F
123462 tftft Test Score: 100 Percentage:100 Grade: A
123463 ftftf Test Score: -50 Percentage:-50 Grade: F
123464 tftt Test Score: 50 Percentage:50 Grade: F
123465 fftt Test Score: 20 Percentage:20 Grade: F
123466 f tt Test Score: 0 Percentage:0 Grade: F
123467 tf Test Score: 40 Percentage:40 Grade: F
123468 tftf Test Score: 80 Percentage:80 Grade: B
123469 f Test Score: -10 Percentage:-10 Grade: F
123470 tt Test Score: 10 Percentage:10 Grade: F
123471 ff Test Score: 10 Percentage:10 Grade: F
123472 ffff Test Score: 20 Percentage:20 Grade: F
123473 t t t Test Score: 60 Percentage:60 Grade: D
123474 fffff Test Score: 10 Percentage:10 Grade: F
123475 ftftt Test Score: -20 Percentage:-20 Grade: F
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.