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;
}

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++)
}
int main() {

ifstream inputFile;
ifstream ansFile;
inputFile.open("C:\\CS110\\student.txt");

string ans;
string solutions;
while(!ansFile.eof()) {
ansFile >> solutions;
}
int k=0;
cout << "STUDENT ID:\tANSWERS:\t\tSCORE:\tLETTER:" << endl;
while(!inputFile.eof()) {
string ID;
//inputFile >> ID >> answers; -ignore!
//getline(inputFile,ID_ans); -ignore!
while(!inputFile.getline(answers ,ID_ans)){  //****THIS IS THE PROBLEM HERE!!*****
cout << ID << "\t" << answers << "\t" << score << "\t" << letter << endl;
++k;
}
cout << endl;

inputFile.close();
ansFile.close();
return 0;
}``````

## All 8 Replies

Hope the following code. helps.

``````int main ()
{

ifstream inputFile ("C:/inputFile.txt");
if ( !inputFile.is_open ())
{
cout<<"File not opened"<<endl;
}

string line;
while (std::getline (inputFile,line, '\n'))
{
std::stringstream inStream(line);
inStream>>userId;

}
inputFile.close ();

return 0;
}``````

I was getting an error but I realized I had not had the <sstream> library added, it works now thanks alot! I appreciate the fast response.

Hmmm, I tried this but the result isn't what is needed, it only reads the file and reputs the file out in the output of the program, it doesn't go through the functions and adds the Ts and F's and Spaces

No it's not, but it's a helping hand. In the code given each line is stored (in a loop) in the string "answers". So what you need to do is combine the two programs so that your own function will parse these strings

exactly xsoniccrackersx, I tried to provide you the strings separately now you should modify the algorithm yourself or atleast paste you efforts :).

I have but I have an error that I cannot figure out.

error: conversion from 'void (*)(std::string, std::string&, std::string&)' to non-scalar type 'std::string' requested

``````#include <iostream>
#include <string>
#include <sstream>
#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;
}

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++)
}
int main()
{
ifstream inputFile;
ifstream ansFile;
inputFile.open("Users/AlfB/Desktop/Student.txt");
string ans;
string solutions;
while(!ansFile.eof()) {
ansFile >> solutions;
}
int k=0;
cout << "STUDENT ID:\tANSWERS:\t\tSCORE:\tLETTER:" << endl;
while(!inputFile.eof()) {
string ID;
while (std::getline (inputFile,line, '\n')){
std::stringstream inStream(line);
inStream>>ID;
ID_ans(ID_ans,answers, solutions); // ERROR OCCURS HERE!!!!!!!!!! :(
cout << ID << "\t" << answers << "\t" << score << "\t" << letter << endl;
++k;
}
cout << endl;

inputFile.close();
ansFile.close();
return 0;
}``````

check the following program

``````char CalculateGrade (const double percent)
{

if (percent >= 90)
return 'A';
else if (percent >= 80)
return 'B';
else if (percent >= 70)
return 'C';
else if (percent >= 60)
return 'D';
else if (percent >= 50)
return 'E';
else
return 'F';
}

{
double  score = 0;

for (size_t i=0; i< answers.length (); ++i)
{
{
++score;
}
else
{
--score;
}
}

return percent;
}

int main ()
{

ifstream inputFile ("C:/inputFile.txt");

if ( !inputFile.is_open ())
{
cout<<"File not opened"<<endl;
}

{
}

string line;
string userId, studentResponse;
while (std::getline (inputFile,line, '\n'))
{
// you have student's answer in variable -->studentResponse, and id --> userId.
std::stringstream inStream(line);
inStream>>userId;
inStream.get();
std::getline(inStream, studentResponse, '\n');

}

inputFile.close ();

return 0;
}``````