| | |
Problem with getline with input files
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 5
Reputation:
Solved Threads: 0
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****
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****
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by xsoniccrackersx; Apr 4th, 2008 at 3:11 am.
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
Hope the following code. helps.
C++ Syntax (Toggle Plain Text)
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; }
•
•
Join Date: Apr 2008
Posts: 5
Reputation:
Solved Threads: 0
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
error: conversion from 'void (*)(std::string, std::string&, std::string&)' to non-scalar type 'std::string' requested
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
check the following program
The answers File contains answers in the form
TFTFTFFFF.
C++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Need help with Dijkstra's Algorithm (C++)
- fstream Tutorial (C++)
- Binary Inputs and Outputs *Files Questions (C++)
- Writing stuff inside files in C++ bloodshed (C++)
- Finished my code, it compiles, but when i use build bersion, there is always an error (C++)
- Problem with strtok() for data structure creation (C++)
- vc++ mfc-i can't make getline work with a string for file input (C++)
- Using data i read from *.* files (C++)
- need help with fstream input files (C++)
Other Threads in the C++ Forum
- Previous Thread: HI all
- Next Thread: How to sort??
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






. 