•
•
•
•
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
![]() |
Good day. My infile contains the following:
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.
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
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.
>...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:
>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...
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:
cplusplus Syntax (Toggle Plain Text)
#include <cstdlib> #include <cstring> #include <fstream> #include <iostream> namespace { const int NQUESTIONS = 22; } int main() { std::ifstream in ( "sourcefile" ); if ( !in ) { std::cerr<<"File open failure\n"; return EXIT_FAILURE; } else { char answers[23]; // Get the correct answers if ( !in.getline ( answers, sizeof answers ) || std::strlen ( answers ) != NQUESTIONS ) { std::cerr<<"Invalid file format\n"; return EXIT_FAILURE; } // Process each subsequent record } return EXIT_SUCCESS; }
>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.
•
•
Join Date: Jul 2005
Posts: 1,288
Reputation:
Rep Power: 9
Solved Threads: 175
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.
>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.
>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.
•
•
Join Date: Jul 2005
Posts: 1,288
Reputation:
Rep Power: 9
Solved Threads: 175
>>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.
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.
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
-- Pearl Williams
•
•
Join Date: Jul 2005
Posts: 1,288
Reputation:
Rep Power: 9
Solved Threads: 175
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.
•
•
Join Date: Jul 2005
Posts: 1,288
Reputation:
Rep Power: 9
Solved Threads: 175
Tested basic code below:
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().
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().
Good day:
Considering the fact that my profesor requires functions, i thought i'd have it like this. Thx for your input.
cplusplus Syntax (Toggle Plain Text)
#include <iomanip> #include <cmath> #include <fstream> #include<string> #include<iostream> using namespace std; void initialize(char test, int sstudent[30]); void getdata(char test, int sstudent[30]); int main() { ifstream inFile; ofstream outFile; inFile.open ("tftest.txt"); outFile.open ("testout"); int i, j, k; int total=0; char test; int student[30]; initialize(test,student); getdata(test,student); for(i = 2; i < 30; i = i + 2) { for(j = 0; j < 22; j++) { cout << student[0].answer.substr(j,1); if(student[0].answer.substr(j,1) == student[i].answer.substr(j,1)) { total = total + 5; }//end if else if(student[i].answer.substr(j,1) == " ") { total = total - 4; }//end else else { total = total - 2; }//end else }//end for }//end for return 0; }// end of main function //********************************** Function Definitions void initialize(test sstudent[30]) { int i; for(i = 1; i < 30; i++) { sstudent[i].id = ""; sstudent[i].answer = ""; }//end for }//end initialize //*********************************** void getdata(test sstudent[30]) { int i; getline(infile, sstudent[0].answer); for(i = 1; i < 30; i++) { getline(infile, sstudent[i].id); getline(infile, sstudent[i].answer); }//end for infile.close(); }//end getdata //************************************
Last edited by zandiago : Oct 26th, 2007 at 12:33 pm. Reason: tags
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Finding the lowest of five test scores (C++)
- need help for saving high scores (C++)
- Loops (C++)
- can't get CPU to do 100% load in XP (Windows NT / 2000 / XP / 2003)
- Using UTF's, IndexOf's and substrings (Java)
Other Threads in the C++ Forum
- Previous Thread: Depth first search of a labyrinth
- Next Thread: searching binary search tree



Linear Mode