I am sorry can you expand a little bit on what was going wrong with my code? Thank you so much. I just want to understand what is happening here. I have some idea. but just missing the context of what my code was doing wrong and how your code fixes my mistake. I want to understand what I was logically and systematically doing to get the incorrect output.
Well, this line: getline(inFile, line[SIZE -1], '\n') is reading a line from the file and storing it in the last row of the array;
After that, inFile >> line[count]; is reading the next word (as far as the first whitespace character) into consecutive rows of the array.
So your code was doing something like this:
getline: TFFTFFTTTTFFTFTFTFTT // all of line 1
cin >>: ABC54301 // first part of line 2
getline: TFTFTFTT TFTFTFFTTFT // remainder of line 2
cin >>: ABC64301 // first part of line 3
getline: TFTFTFTT TFTFTFFTTFT // remainder of line 3
cin >>: ABC74301 // first part of line 4
etc...
This while unexpected it would honestly work out good for me. would there be a reliable way to access the "remainders" given the set of code and and input file? Thanks again for your input. This is very helpful.
Rather than an array, I used a vector here. For my own convenience in testing, I used a stringstream rather than a file, but the behaviour of both types of stream should be equivalent.
Here is my final soution. Thanks for your help. It gave me the traction I needed!
Here is the explanation out of the book I am going through.
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; and 0%–59.99%, F
#include <iostream>
#include <fstream>
#include <string>
#include <array>
#define SIZE 64
usingnamespace std;
char grader(string studentsoution, string actualsoution);
int main(int argc, constchar * argv[])
{
ifstream inFile;
inFile.open("inFile.txt", ifstream::in);
if (!inFile) {
cerr << "Error: in file could not be opened" << endl;
return -1;
}
// open file and read line by line.
int count = 0;
string line[SIZE];
if (inFile.is_open()) {
while (getline(inFile, line[count++], '\n')) {
}
inFile.close();
}
//splitting line into sub strings.
string studentID[SIZE], studentSolutions[SIZE];
for (int i = 1; i < count -1 ; i++) {
studentSolutions[i] = line[i].substr(9);
studentID[i] = line[i].substr(0,8);
cout << studentID[i] << " points grade is... "
<< grader(studentSolutions[i].c_str(), line[0].c_str())
<< endl;
}
return 0;
}
char grader(string studentsoution, string actualsoution){
int DeductPoints = 0;
for (int i = 0; i < actualsoution.length(); i++) {
if ( studentsoution.at(i) != actualsoution.at(i)) {
DeductPoints++;
i++;
}
}
double totalpoints = 20 - DeductPoints;
int grade = totalpoints / 20*100;
char letterGrade = '\0';
switch(grade/10)
{
case 9: letterGrade = 'A';
break;
case 8: letterGrade = 'B';
break;
case 7: letterGrade = 'C';
break;
case 6: letterGrade = 'D';
default:
letterGrade = 'F';
}
return letterGrade;
}
output
1 2 3 4 5 6 7 8
ABC54301 points grade is... A
ABC64301 points grade is... C
ABC74301 points grade is... F
ABC84301 points grade is... C
ABC94301 points grade is... C
ABC14301 points grade is... F
ABC24301 points grade is... C
ABC34301 points grade is... F