I'm so close to finishing this program. Please offer your assistance, if you are skilled. ^_^
Here is the Question/Problem I am attempting to solve.
"The history teacher at your school needs help in grading a True/False test. The students' IDs and test answers are stored in a file. The first entry in the file contains answers to the test in the form:
TFFTFFTTTTFFTFTFTFTT
Every other entry in the file is the student ID, followed by a blank, followed by the student's responses. For example, the entry...
ABC54301 TFTFTFTT TFTFTFFTTFT
indicates that the student ID is ABC54301 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 following grade scale: 90%-100%, A; 80%-89.99%, B; 70%-79.99%, C; 60%-69.99%, D; 0%-59.99%, F."
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
usingnamespace std;
int main()
{
string id;
char answers [20];
char response;
char fileName [25];
int testScore;
cout << "\nThis program will take the teacher's correct test answers";
cout << "\nAs well as the 150 students answers, and grade the tests.\n" << endl;
cout << "Enter the input file name: ";
cin >> fileName;
ifstream inFile;
inFile.open (fileName);
inFile.close();
for (int i=0; i<20; i++)
inFile >> answers [i];
while ((inFile >> id))
{
cout << "\n" << id << " ";
inFile.get(response);
testScore = 0;
for (int i = 0; i < 20; i++)
{
inFile.get(response);
cout << " " << response;
if (response == ' ')
testScore += 0;
elseif (response == answers [i])
testScore += 2;
else
testScore += -1;
}
cout << " " << testScore << " ";
double p = testScore * 2.5;
if (p >= 90)
cout << 'A';
elseif (p >=80)
cout << 'B';
elseif (p >=70)
cout << 'C';
elseif (p >=60)
cout << 'D';
else
cout << 'F';
}
cout << "\nStudent IDs | Answers | % | Grades";
cout << "\n--------------------------------------------" << endl;
return 0;
}
When I run the program, the output is... This program will take the teacher's correct test answers
As well as the 150 students answers, and grade the tests.
Enter the input file name: grades.txt
Student IDs | Answers | % | Grades
--------------------------------------------
Yet there should be 150 Student IDs, their Answers, % correct, and their Grade.
You read the file one character at a time. You never account for newlines. You never attempt to read in a student id, treating all characters you encounter after the first set of answers as quiz answers.
Feel free to completely remove inFile.close(), since the file will be automatically closed by the destructor. It definitely would seem your program is not finding the file. Make sure you've got it in the right place.
Feel free to completely remove inFile.close(), since the file will be automatically closed by the destructor. It definitely would seem your program is not finding the file. Make sure you've got it in the right place.
That's good to know. Tutorials I have done in other languages always emphasised closing the file so that's why I made the comment.
Put a break point @ while ((inFile >> id)) verify you are reading the id on the first and second pass... Check a few responses as well. Report back your findings.