Hi all,
I'm new here but I am in need of some desperate help. I have a program that works perfectly but here is the problem: I cannot use the function getline to get a line in the input file and turn it into a string to be manipulated by the ID_ans function.
The problem is as follows:
Write a program for grading True/False test. The student ID and answers are stored in a file. The first entry in the file contains answers to the test in the form TFFTFFTTTTFTFTFTFTFT
Every other entry in the file is the student ID, followed by a blank, followed by the student's response. For example, the entry ABC54301 TFTFTFTT TFTFTFFTTFT indicates 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. Each correct answer is awarded two points, each wrong answer get -1 point, and no answer gets 0 points.The output should be: the student ID, followed by the answers, followed by the test score, followed by the test grade. Assume the following grade scaling:
A: 90% - 100%
B: 80% - 89.99%
C: 70% - 79.99%
D: 60% - 69.99%
F: 0% - 59.99%
Any help will be greatly appreciated, thank you in advance!
Oh by the way, in the main function is the problem, ignore the two lines I put -ignore by as they were code commented out before using the ID_ans function. The problem is with the getline code that i outlined with ****THIS IS THE PROBLEM HERE****
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int getScore(string ans, string sol) {
int score=0;
for(int k=0; k<20; ++k) {
if(ans[k]==sol[k])
score+=2;
else if(ans[k]==' ')
score+=0;
else
--score;
}
return score;
}
char getGrade(int score) {
double percent=(score/40.0)*100;
if(percent>=90)
return 'A';
else if(percent>=80 && percent<89.99)
return 'B';
else if(percent>=70 && percent<79.99)
return 'C';
else if(percent>=60 && percent<69.99)
return 'D';
else
return 'F';
}
void ID_ans (string combined, string &ID, string &answer)
{
for (int counter=0;counter <9; counter++)
ID[counter]= combined[counter];
for (int counter=9; counter < 30; counter++)
answer[counter] = combined[counter];
}
int main() {
ifstream inputFile;
ifstream ansFile;
inputFile.open("C:\\CS110\\student.txt");
ansFile.open("C:\\CS110\\answers.txt");
string ans[20];
string solutions;
while(!ansFile.eof()) {
ansFile >> solutions;
}
int k=0;
cout << "STUDENT ID:\tANSWERS:\t\tSCORE:\tLETTER:" << endl;
while(!inputFile.eof()) {
string answers;
string ID;
//inputFile >> ID >> answers; -ignore!
//getline(inputFile,ID_ans); -ignore!
while(!inputFile.getline(answers ,ID_ans)){ //****THIS IS THE PROBLEM HERE!!*****
ID_ans(ID_ans,answers, solutions);
double score = getScore(answers, solutions);
char letter = getGrade(score);}
cout << ID << "\t" << answers << "\t" << score << "\t" << letter << endl;
ans[k]=answers;
++k;
}
cout << endl;
inputFile.close();
ansFile.close();
return 0;
}