943,884 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3869
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 25th, 2007
0

c++ scores

Expand Post »
Good day. My infile contains the following:
C++ Syntax (Toggle Plain Text)
  1. TFFTFFTFFTFFTFTFTFTFFF
  2. A33
  3. TFFTFFTFFFFFTFTFTFFFFT
  4. Z27
  5. TFFTFFTFFTFFTFTFTFTFFT
  6. X12
  7. TFTT FTFFTFFFT FTFTFFF
  8. H44
  9. TFFTFFTFFTFFFFTFTFFTFF
  10. Q19
  11. FFFFFFFFFFFFTFTFTFFFFF
  12. D72
  13. TFFTFFTFFTFF FFFFFTFFF
  14. W32
  15. FFFTFFTFFTFFTFTFTFFTTT
  16. Y09
  17. TFFTFFTFFTFFTFFTTFFTTT
  18. S44
  19. TFFTFFTFFTFFTFTFTFTFFF
  20. G11
  21. FFTFFTFFTFFTFTFTTFTFFF
  22. J21
  23. TFFTFFFFFTFFTFTFTFTFTT
  24. K61
  25. TFFTFFTFFTFFTFTFTFTFT
  26. M03
  27. TFFTFFTFFFFFTFTFTFTFFT
  28. P24
  29. TFFTFFTFFTFFTFTFTFFFFT
  30. N54
  31. FTFFTFFTFT TFF FTFTFFF
  32. F33
  33. TFFTFFTFFTFFTFTFTFTFFF
  34. Z21
  35. TFFTFFFTTFTTFFTFTFTFTF
  36. V39
  37. TFFFTTFTTFTTFTFTFTTFFF
  38. O66
  39. TTFTFFTFFTFFTFTFTFTFFF
  40. B29
  41. TFFTFFTFFTFF FFTTFFTFF
  42. J17
  43. TFFTFFFTTTFFTFFTFTFTTT
  44. K09
  45. TFFTFFTFTTFTTFTFTFFTFF
  46. L99
  47. FFTFFTFFTFFFFTFTFTFFF
My assignment:
The history teacher at the school needs help in grading a True/False test. The
student lD’s and test answers are stored (vertically) in a file named "tftest.txt". The
first entry in the file contains answers to the test in the form
TFFTFFTTTTFFTFTFTFTFTT
All of the other entries in the file are data for a student on 2 lines:
A43
TFFTFFTT TFTFTFFTTFTTT
Note that this student did not answer question 9. The test has 22 questions and
there are not more than 50 students in the class. Each correct answer is awarded 5
points, each incorrect answer is awarded -2 points (negative) and each "no answer"
gets a value of -4 points (negative).
lt can be seen that all test scores will be a whole number.
The student in the example achieved a grade as follows:
14 correct * 5 = 70
7 incorrect * -2 = -14
1 no answer *-4 = -4
70 - 14 -4 = 52 F
His output line would look like:
A43 TFFTFFTT TFTFTFFTTFTTT 52 F
The output should be the student’s ID, the student’s answers followed by the test
score and the test grade. The grade scale is:
Above 90 = A
80 - 89 = B
70 - 79 = C
60 - 69 = D
Below 60 = F
The program should finally display the class average (2 decimals)
...Now where do i even begin? Since the 1st line contains the correct answer...how do i store them so that the other lines are compared to it for the correct answer. I really need a jumpstart with this one. If i use a getline function for example...how will it diffrentiate between the answers i wanna check and the student's id #?
Thanks for all the pointers & input.
Similar Threads
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 25th, 2007
0

Re: c++ scores

>...Now where do i even begin?
You could use everything you've learned in your previous threads that do something similar.

>how do i store them so that the other lines are compared to it for the correct answer.
Just store it in an array:
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <cstring>
  3. #include <fstream>
  4. #include <iostream>
  5.  
  6. namespace {
  7. const int NQUESTIONS = 22;
  8. }
  9.  
  10. int main()
  11. {
  12. std::ifstream in ( "sourcefile" );
  13.  
  14. if ( !in ) {
  15. std::cerr<<"File open failure\n";
  16. return EXIT_FAILURE;
  17. }
  18. else {
  19. char answers[23];
  20.  
  21. // Get the correct answers
  22. if ( !in.getline ( answers, sizeof answers )
  23. || std::strlen ( answers ) != NQUESTIONS )
  24. {
  25. std::cerr<<"Invalid file format\n";
  26. return EXIT_FAILURE;
  27. }
  28.  
  29. // Process each subsequent record
  30. }
  31.  
  32. return EXIT_SUCCESS;
  33. }
>If i use a getline function for example...how will it diffrentiate
>between the answers i wanna check and the student's id #?
Obviously because the ID numbers and scores are on two different lines. You read the ID first, then on the next line start processing scores...
Last edited by Ptolemy; Oct 25th, 2007 at 1:15 pm.
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Ptolemy is offline Offline
62 posts
since Oct 2007
Oct 25th, 2007
0

Re: c++ scores

Each line in the file could be read in as a string. The first string would be stored as the correct answer string. The remaining lines alternate between student ID strings and the student answer strings. Once you have a student answer string you compare each element of that string with each element of the correct answer string and keep track of all the appropriate pieces of information with variables that act as counters.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 25th, 2007
0

Re: c++ scores

>Once you have a student answer string you compare each element
>of that string with each element of the correct answer string
But from the sample file it looks like if the last N answers were blank, the string is trimmed by that many characters. You also have to keep track of where you hit the end of the student answer string and subtract the correct number of "no answer" values from the score.
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Ptolemy is offline Offline
62 posts
since Oct 2007
Oct 25th, 2007
0

Re: c++ scores

>>But from the sample file it looks like if the last N answers were blank, the string is trimmed by that many characters. You also have to keep track of where you hit the end of the student answer string and subtract the correct number of "no answer" values from the score.

True enough. But I like it better than using sequential calls to something like get(), which would read the file one char at a time without ignoring whitespace, which is the alternative I can think of off the top of my head.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 25th, 2007
0

Re: c++ scores

Oh, don't read it as me bashing your solution, it's the same one I'd use. I was just mentioning that little pitfall because I'd noticed it while brainstorming.
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Ptolemy is offline Offline
62 posts
since Oct 2007
Oct 25th, 2007
0

Re: c++ scores

Personally, I'd read the test answers into a char* rather than a C++ string. A char* must be defined to accommodate the total number of answers necessary, but a string will read only the answers on the line. If a student doesn't answer the last 5 questions, the string could be short and cause problems. A char* will still have data, however it will have to be cleared after each use.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Oct 25th, 2007
0

Re: c++ scores

If there's supposed to be 20 answers but the last question is unanswered then I suppose it will depend on whether there is a space before the newline char at the end of the line in the file or whether the newline char is right after the T/F for answer 19. If the newline char is right after the answer for question 19 and the line is then read in as a char *, or say char line[21], I wonder whether there would be a null char at line[19] or a space at line[19]? Guess I'll have to try it out for myself when I get the chance.
Last edited by Lerner; Oct 25th, 2007 at 9:13 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 25th, 2007
0

Re: c++ scores

Tested basic code below:
C++ Syntax (Toggle Plain Text)
  1. ofstream fout("practiceText");
  2. char first[6] = "TTTT "; //"TTTT";
  3. fout << first << endl;
  4. fout.close();
  5.  
  6. ifstream fin("practiceText");
  7.  
  8. char first2[6];
  9. fin.getline(first2, 6);
  10.  
  11. //string first2;
  12. //getline(fin, first2);
  13.  
  14. cout << first2 << endl;
  15.  
  16. if(first2[4] == ' ')
  17. cout << "space" << endl;
  18. else
  19. cout << "no space" << endl;
  20.  
  21. fin.close();

Using "TTTT " the version with first2 declared as char[] and string both output space whereas with "TTTT" the version with first2 declared as char[] and string both output no space.

Therefore, if file is set up with trailing spaces as needed for unanswered questions at end of answer line then reading in as either string or char[] should pick up that information whereas if file is set up with newline right after last T/F in the line, then allowances must be made irrespective of type read into. In addition, there's no apparent benefit to reading in answer line char by char with get() instead of with the appropriate version of getline().
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 26th, 2007
0

Re: c++ scores

Good day:
C++ Syntax (Toggle Plain Text)
  1. #include <iomanip>
  2. #include <cmath>
  3. #include <fstream>
  4. #include<string>
  5. #include<iostream>
  6.  
  7. using namespace std;
  8.  
  9. void initialize(char test, int sstudent[30]);
  10. void getdata(char test, int sstudent[30]);
  11.  
  12. int main()
  13.  
  14. {
  15.  
  16. ifstream inFile;
  17. ofstream outFile;
  18.  
  19. inFile.open ("tftest.txt");
  20. outFile.open ("testout");
  21.  
  22. int i, j, k;
  23. int total=0;
  24.  
  25. char test;
  26. int student[30];
  27. initialize(test,student);
  28. getdata(test,student);
  29.  
  30. for(i = 2; i < 30; i = i + 2)
  31. {
  32. for(j = 0; j < 22; j++)
  33. {
  34. cout << student[0].answer.substr(j,1);
  35.  
  36. if(student[0].answer.substr(j,1) == student[i].answer.substr(j,1))
  37. {
  38. total = total + 5;
  39. }//end if
  40. else if(student[i].answer.substr(j,1) == " ")
  41. {
  42. total = total - 4;
  43. }//end else
  44. else
  45. {
  46. total = total - 2;
  47. }//end else
  48. }//end for
  49.  
  50.  
  51.  
  52. }//end for
  53.  
  54. return 0;
  55. }// end of main function
  56. //********************************** Function Definitions
  57. void initialize(test sstudent[30])
  58. {
  59. int i;
  60. for(i = 1; i < 30; i++)
  61. {
  62. sstudent[i].id = "";
  63. sstudent[i].answer = "";
  64.  
  65. }//end for
  66. }//end initialize
  67. //***********************************
  68. void getdata(test sstudent[30])
  69. {
  70. int i;
  71.  
  72. getline(infile, sstudent[0].answer);
  73.  
  74. for(i = 1; i < 30; i++)
  75. {
  76. getline(infile, sstudent[i].id);
  77. getline(infile, sstudent[i].answer);
  78.  
  79. }//end for
  80.  
  81. infile.close();
  82.  
  83. }//end getdata
  84. //************************************
Considering the fact that my profesor requires functions, i thought i'd have it like this. Thx for your input.
Last edited by zandiago; Oct 26th, 2007 at 12:33 pm. Reason: tags
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007

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: Depth first search of a labyrinth
Next Thread in C++ Forum Timeline: searching binary search tree





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


Follow us on Twitter


© 2011 DaniWeb® LLC