User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,559 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,457 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2190 | Replies: 40 | Solved
Reply
Join Date: Jun 2007
Location: Home
Posts: 2,451
Reputation: zandiago is on a distinguished road 
Rep Power: 6
Solved Threads: 24
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

c++ scores

  #1  
Oct 25th, 2007
Good day. My infile contains the following:
TFFTFFTFFTFFTFTFTFTFFF
A33
TFFTFFTFFFFFTFTFTFFFFT
Z27
TFFTFFTFFTFFTFTFTFTFFT
X12
TFTT FTFFTFFFT FTFTFFF
H44
TFFTFFTFFTFFFFTFTFFTFF
Q19
FFFFFFFFFFFFTFTFTFFFFF
D72
TFFTFFTFFTFF FFFFFTFFF
W32
FFFTFFTFFTFFTFTFTFFTTT
Y09
TFFTFFTFFTFFTFFTTFFTTT
S44
TFFTFFTFFTFFTFTFTFTFFF
G11
FFTFFTFFTFFTFTFTTFTFFF
J21
TFFTFFFFFTFFTFTFTFTFTT
K61
TFFTFFTFFTFFTFTFTFTFT 
M03
TFFTFFTFFFFFTFTFTFTFFT
P24
TFFTFFTFFTFFTFTFTFFFFT
N54
FTFFTFFTFT TFF FTFTFFF
F33
TFFTFFTFFTFFTFTFTFTFFF
Z21
TFFTFFFTTFTTFFTFTFTFTF
V39
TFFFTTFTTFTTFTFTFTTFFF
O66
TTFTFFTFFTFFTFTFTFTFFF
B29
TFFTFFTFFTFF FFTTFFTFF
J17
TFFTFFFTTTFFTFFTFTFTTT
K09
TFFTFFTFTTFTTFTFTFFTFF
L99
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: c++ scores

  #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  
Join Date: Jul 2005
Posts: 1,288
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 175
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: c++ scores

  #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  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: c++ scores

  #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  
Join Date: Jul 2005
Posts: 1,288
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 175
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: c++ scores

  #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  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: c++ scores

  #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  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: c++ scores

  #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.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Jul 2005
Posts: 1,288
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 175
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: c++ scores

  #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  
Join Date: Jul 2005
Posts: 1,288
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 175
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: c++ scores

  #9  
Oct 25th, 2007
Tested basic code below:
ofstream fout("practiceText");
char first[6] = "TTTT ";  //"TTTT";
fout << first << endl;
fout.close();

ifstream fin("practiceText");

char first2[6];
fin.getline(first2, 6);

//string first2;
//getline(fin, first2);

cout << first2 << endl;

if(first2[4] == ' ')
    cout << "space" << endl;
else 
    cout << "no space" << endl;

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  
Join Date: Jun 2007
Location: Home
Posts: 2,451
Reputation: zandiago is on a distinguished road 
Rep Power: 6
Solved Threads: 24
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: c++ scores

  #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. //************************************
  85.  
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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:38 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC