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

Hope the following code. helps.

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

	string line;
	string userId, answers;
	while (std::getline (inputFile,line, '\n'))
	{
		std::stringstream inStream(line);
		inStream>>userId;
		std::getline(inStream, answers, '\n');
		cout<<"The userId is :"<<userId<<" and answers are: "<<answers<<endl;
		
	}
	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;
}

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("Users/AlfB/Desktop/Student.txt");
	ansFile.open("Users/AlfB/Desktop/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, line;
		string ID;
		while (std::getline (inputFile,line, '\n')){
		std::stringstream inStream(line);
		inStream>>ID;
		std::getline(inStream, answers, '\n');
		ID_ans(ID_ans,answers, solutions); // ERROR OCCURS HERE!!!!!!!!!! :(
		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;
}

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 CalculateAnswers (const string& studentsResponse, const string& answers)
{
	double  score = 0;

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

	double percent = (score/answers.length ())*100;
	return percent;
}

int main () 
{
	
	ifstream inputFile ("C:/inputFile.txt");
	ifstream answersFile ("C:/answers.txt");

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

	if (!answersFile.is_open ())
	{
		cout<<"Answers File not Opened"<<endl;
	}

	

	string answer;				//read from file...
	// Reading the answer from answers File.
	std::getline (answersFile, answer, '\n');
	answersFile.close ();
	answersFile.clear ();

	// Reading the Input File...
	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');
		
		//Compare the answers.
		double score = CalculateAnswers (studentResponse, answer);
		
		// Calculate the Grade.
		char grade = CalculateGrade (score);

		cout<<"The Student :"<<userId<<" got "<<grade<<" grade"<<endl;
		
	}

	inputFile.close ();

	return 0;
}

The answers File contains answers in the form
TFTFTFFFF.

Wow thanks alot Laiq Ahmed's that worked out great for me. thanks so much I really appreciate it. The code works now! You have no idea how much I appreciate you taking the time to help me with my issues. If theres anything I can do let me know!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.