The history teacher at your school needs help in grading a True/False test with
ID: 3715052 • Letter: T
Question
The history teacher at your school needs help in grading a True/False test with 5 questions for a total of 25 students. The students’ ID numbers and test answers are stored in a file called "input_8.txt". The first entry in the file contains answer key 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 answer. 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). Your goal is to create an output file, that has the studentsID, answer and their score and letter grade printed in a table.
Your program should read the studentID and the test answer for each of the students from the file provided (input_8) and store the data in arrays. Remember to use file error checking and close files when done. Hint: Use the getline() function to read the key, student IDs and answers.
You have to write a function, that computes the marks scored by the students as follows. The function should take two parameters of the type string. The first parameter should be the answer key, and the second parameter should be the students' answer. The function returns the score, which is computed as follows. Each correct answer is awarded twenty points, each wrong answer gets –10 point, and no answer (indicated by a blank) gets 0 points. For example, the answer key is "TFFTF", and if the student's answer is "TF TT". The score should be 50. This is computed as follows, 40 pts for the answer 1 and 2 which are correct, 0 for the third answer which is blank, 20 pts for the 4th answer which is correct, and -10 for the last answer which is incorrect. Your main program should call this function to compute the score for each student.
Next write a second function that determines the letter 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. The function has one input parameter of type int, which is the score on the test. The function should return a string data type which is the letter grade. Your main program should call this function when printing the letter grade to the output file.
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 their answers, followed by the test score, followed by the test grade. 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 (output_8.txt).
Below is a snippet from the input file:
Sample OUTPUT
(input_8.txt is copy and pasted right here, might need to save the text at the bottom as input_8.txt as well as putting it in same folder as .cpp for it to work)
TFFTF 224461#TFFTF 309223#TFFFF 897785#TFTTF 338577#TFTFT 318631#FFTTF 484006#TF TF 591747# TTFT 994595#TTFTF 678984#TFFTFExplanation / Answer
Below is your code
//Program.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
string first, str;
int i=0, j=0 , x;
float sum=0.0;
char grade;
// reading a text file
ifstream myfile ("input.txt");
if (myfile.is_open())
{
cout<<"Student ID "<<"Answer "<<"Marks "<<"Grade "<<" ";
while ( getline (myfile,line) )
{
sum = 0;
if(i==0){
first=line;
i++;
}
else{
cout << ' ';
for(std::string::size_type t = 0; t < line.size(); ++t) {
if(line[t]=='#'){
j=t+1;
x=0;
while(j<line.size()){
if(line[j]==first[x])
sum = sum+20;
else if(line[j]==' ')
sum = sum+0;
else
sum = sum-10;
j++;
x++;
}
if(sum>89.99&&sum<=100)
grade = 'A';
else if(sum>79.99&&sum<=89.99)
grade = 'B';
else if(sum>69.99&&sum<=79.99)
grade = 'C';
else if(sum>59.99&&sum<=69.99)
grade = 'D';
else grade = 'F';
cout<<line.substr(0,6)<<" "<<line.substr(7,5)<<" "<<sum<<" "<<grade<<" ";
break;
}
}}
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.