943,737 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2706
  • C++ RSS
Apr 4th, 2008
0

Problem with getline with input files

Expand Post »
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****

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int getScore(string ans, string sol) {
  9. int score=0;
  10. for(int k=0; k<20; ++k) {
  11. if(ans[k]==sol[k])
  12. score+=2;
  13. else if(ans[k]==' ')
  14. score+=0;
  15. else
  16. --score;
  17. }
  18. return score;
  19. }
  20.  
  21. char getGrade(int score) {
  22. double percent=(score/40.0)*100;
  23. if(percent>=90)
  24. return 'A';
  25. else if(percent>=80 && percent<89.99)
  26. return 'B';
  27. else if(percent>=70 && percent<79.99)
  28. return 'C';
  29. else if(percent>=60 && percent<69.99)
  30. return 'D';
  31. else
  32. return 'F';
  33. }
  34.  
  35. void ID_ans (string combined, string &ID, string &answer)
  36. {
  37. for (int counter=0;counter <9; counter++)
  38. ID[counter]= combined[counter];
  39. for (int counter=9; counter < 30; counter++)
  40. answer[counter] = combined[counter];
  41. }
  42. int main() {
  43.  
  44. ifstream inputFile;
  45. ifstream ansFile;
  46. inputFile.open("C:\\CS110\\student.txt");
  47. ansFile.open("C:\\CS110\\answers.txt");
  48.  
  49. string ans[20];
  50. string solutions;
  51. while(!ansFile.eof()) {
  52. ansFile >> solutions;
  53. }
  54. int k=0;
  55. cout << "STUDENT ID:\tANSWERS:\t\tSCORE:\tLETTER:" << endl;
  56. while(!inputFile.eof()) {
  57. string answers;
  58. string ID;
  59. //inputFile >> ID >> answers; -ignore!
  60. //getline(inputFile,ID_ans); -ignore!
  61. while(!inputFile.getline(answers ,ID_ans)){ //****THIS IS THE PROBLEM HERE!!*****
  62. ID_ans(ID_ans,answers, solutions);
  63. double score = getScore(answers, solutions);
  64. char letter = getGrade(score);}
  65. cout << ID << "\t" << answers << "\t" << score << "\t" << letter << endl;
  66. ans[k]=answers;
  67. ++k;
  68. }
  69. cout << endl;
  70.  
  71. inputFile.close();
  72. ansFile.close();
  73. return 0;
  74. }
Last edited by xsoniccrackersx; Apr 4th, 2008 at 3:11 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xsoniccrackersx is offline Offline
5 posts
since Apr 2008
Apr 4th, 2008
0

Re: Problem with getline with input files

Hope the following code. helps.

C++ Syntax (Toggle Plain Text)
  1. int main ()
  2. {
  3.  
  4. ifstream inputFile ("C:/inputFile.txt");
  5. if ( !inputFile.is_open ())
  6. {
  7. cout<<"File not opened"<<endl;
  8. }
  9.  
  10. string line;
  11. string userId, answers;
  12. while (std::getline (inputFile,line, '\n'))
  13. {
  14. std::stringstream inStream(line);
  15. inStream>>userId;
  16. std::getline(inStream, answers, '\n');
  17. cout<<"The userId is :"<<userId<<" and answers are: "<<answers<<endl;
  18.  
  19. }
  20. inputFile.close ();
  21.  
  22. return 0;
  23. }
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Apr 4th, 2008
0

Re: Problem with getline with input files

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xsoniccrackersx is offline Offline
5 posts
since Apr 2008
Apr 4th, 2008
0

Re: Problem with getline with input files

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xsoniccrackersx is offline Offline
5 posts
since Apr 2008
Apr 4th, 2008
0

Re: Problem with getline with input files

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
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Apr 4th, 2008
0

Re: Problem with getline with input files

exactly xsoniccrackersx, I tried to provide you the strings separately now you should modify the algorithm yourself or atleast paste you efforts .
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Apr 4th, 2008
0

Re: Problem with getline with input files

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
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. int getScore(string ans, string sol) {
  10. int score=0;
  11. for(int k=0; k<20; ++k) {
  12. if(ans[k]==sol[k])
  13. score+=2;
  14. else if(ans[k]==' ')
  15. score+=0;
  16. else
  17. --score;
  18. }
  19. return score;
  20. }
  21.  
  22. char getGrade(int score) {
  23. double percent=(score/40.0)*100;
  24. if(percent>=90)
  25. return 'A';
  26. else if(percent>=80 && percent<89.99)
  27. return 'B';
  28. else if(percent>=70 && percent<79.99)
  29. return 'C';
  30. else if(percent>=60 && percent<69.99)
  31. return 'D';
  32. else
  33. return 'F';
  34. }
  35.  
  36. void ID_ans (string combined, string &ID, string &answer)
  37. {
  38. for (int counter=0;counter <9; counter++)
  39. ID[counter]= combined[counter];
  40. for (int counter=9; counter < 30; counter++)
  41. answer[counter] = combined[counter];
  42. }
  43. int main()
  44. {
  45. ifstream inputFile;
  46. ifstream ansFile;
  47. inputFile.open("Users/AlfB/Desktop/Student.txt");
  48. ansFile.open("Users/AlfB/Desktop/Answers.txt");
  49. string ans[20];
  50. string solutions;
  51. while(!ansFile.eof()) {
  52. ansFile >> solutions;
  53. }
  54. int k=0;
  55. cout << "STUDENT ID:\tANSWERS:\t\tSCORE:\tLETTER:" << endl;
  56. while(!inputFile.eof()) {
  57. string answers, line;
  58. string ID;
  59. while (std::getline (inputFile,line, '\n')){
  60. std::stringstream inStream(line);
  61. inStream>>ID;
  62. std::getline(inStream, answers, '\n');
  63. ID_ans(ID_ans,answers, solutions); // ERROR OCCURS HERE!!!!!!!!!! :(
  64. double score = getScore(answers, solutions);
  65. char letter = getGrade(score);
  66. cout << ID << "\t" << answers << "\t" << score << "\t" << letter << endl;
  67. ans[k]=answers;
  68. ++k;
  69. }
  70. cout << endl;
  71.  
  72. inputFile.close();
  73. ansFile.close();
  74. return 0;
  75. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xsoniccrackersx is offline Offline
5 posts
since Apr 2008
Apr 4th, 2008
0

Re: Problem with getline with input files

check the following program

C++ Syntax (Toggle Plain Text)
  1. char CalculateGrade (const double percent)
  2. {
  3.  
  4. if (percent >= 90)
  5. return 'A';
  6. else if (percent >= 80)
  7. return 'B';
  8. else if (percent >= 70)
  9. return 'C';
  10. else if (percent >= 60)
  11. return 'D';
  12. else if (percent >= 50)
  13. return 'E';
  14. else
  15. return 'F';
  16. }
  17.  
  18. double CalculateAnswers (const string& studentsResponse, const string& answers)
  19. {
  20. double score = 0;
  21.  
  22. for (size_t i=0; i< answers.length (); ++i)
  23. {
  24. if (studentsResponse[i] == answers[i])
  25. {
  26. ++score;
  27. }
  28. else
  29. {
  30. --score;
  31. }
  32. }
  33.  
  34. double percent = (score/answers.length ())*100;
  35. return percent;
  36. }
  37.  
  38. int main ()
  39. {
  40.  
  41. ifstream inputFile ("C:/inputFile.txt");
  42. ifstream answersFile ("C:/answers.txt");
  43.  
  44. if ( !inputFile.is_open ())
  45. {
  46. cout<<"File not opened"<<endl;
  47. }
  48.  
  49. if (!answersFile.is_open ())
  50. {
  51. cout<<"Answers File not Opened"<<endl;
  52. }
  53.  
  54.  
  55.  
  56. string answer; //read from file...
  57. // Reading the answer from answers File.
  58. std::getline (answersFile, answer, '\n');
  59. answersFile.close ();
  60. answersFile.clear ();
  61.  
  62. // Reading the Input File...
  63. string line;
  64. string userId, studentResponse;
  65. while (std::getline (inputFile,line, '\n'))
  66. {
  67. // you have student's answer in variable -->studentResponse, and id --> userId.
  68. std::stringstream inStream(line);
  69. inStream>>userId;
  70. inStream.get();
  71. std::getline(inStream, studentResponse, '\n');
  72.  
  73. //Compare the answers.
  74. double score = CalculateAnswers (studentResponse, answer);
  75.  
  76. // Calculate the Grade.
  77. char grade = CalculateGrade (score);
  78.  
  79. cout<<"The Student :"<<userId<<" got "<<grade<<" grade"<<endl;
  80.  
  81. }
  82.  
  83. inputFile.close ();
  84.  
  85. return 0;
  86. }

The answers File contains answers in the form
TFTFTFFFF.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Apr 4th, 2008
0

Re: Problem with getline with input files

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xsoniccrackersx is offline Offline
5 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: HI all
Next Thread in C++ Forum Timeline: How to sort??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC