c++ scores

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

c++ scores

 
0
  #1
Oct 25th, 2007
Good day. My infile contains the following:
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: c++ scores

 
0
  #2
Oct 25th, 2007
>...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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: c++ scores

 
0
  #3
Oct 25th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: c++ scores

 
0
  #4
Oct 25th, 2007
>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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: c++ scores

 
0
  #5
Oct 25th, 2007
>>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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: c++ scores

 
0
  #6
Oct 25th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: c++ scores

 
0
  #7
Oct 25th, 2007
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: c++ scores

 
0
  #8
Oct 25th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: c++ scores

 
0
  #9
Oct 25th, 2007
Tested basic code below:
  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().
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: c++ scores

 
0
  #10
Oct 26th, 2007
Good day:
  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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC