| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
scoring Students
I need to write a program using arrays and strings.Here is the question.
A class of students takes a 20 question multiple-choice exam;each question has 5 choices(a,b,c,d,e)only one of them are correct.In "tests.dat"Each record of which consists of a student id, followed by a blank, followed by students 20 responses. In "answers.dat" which consists of a single record, namely the correct answers to the exam.Then It needs to produce a grade summary report like this.If the answer is correct it should put a "*" by the correct answer student-id number correct 1231231212312 12 1233424325435 25 .... ... question A B C D E 1 5 1 13* 3 1 2 4 7* 5 12 7 . . ... this is what i got so far.Thank you #include <fstream> #include <iostream> #include <string> #include <cstdlib> using namespace std; int question[20][5]; int main() { ifstream fanswers, fcorrect_keys; char correct_keys[21]; char student_id[100][12], answers[100][21]; char id_buf[12], answers_buf[21]; int student_count = 0; fcorrect_keys.open("Answers.dat"); while(!fcorrect_keys.eof()) { fcorrect_keys >> answers_buf; strcpy(correct_keys, answers_buf); } // cout << correct_keys << endl; fcorrect_keys.close(); fanswers.open("Tests.dat"); while(!fanswers.eof()) { fanswers >> id_buf >> answers_buf; strcpy(student_id[student_count], id_buf); strcpy(answers[student_count],answers_buf); student_count++; } // for(int i=0; i < student_count;i++) // cout << student_id[i] << " " << answers[i] << endl; fanswers.close();// close the file cout << " student_id number_correct" << endl << endl; for(int i = 0; i < student_count; i++) { int correct_count = 0; for(int j = 0; j < 20; j++) { if (answers [i][j] == correct_keys[j]) correct_count++; question[j][answers[i][j] - 'A']++; } printf("%11.11s %2d\n", student_id[i],correct_count); } cout << endl << endl << endl << "Number of students taking exam : " << student_count << endl; printf("\nquestion A B C D E\n"); for(int k = 0; k < 20 ; k++) { printf(" %2d ",1+k); for(int l=0;l < 5; l++) printf(" %2d%c ", question[k][l], (l == (correct_keys[k] - 'A') ? '*' : ' ')); printf("\n"); } return 0; } |
|
#3
|
|||
|
|||
|
Re: scoring Students
actually I wrote my code and I know what I did wrong only I dont know how to change it.
|
|
#4
|
|||
|
|||
|
Re: scoring Students
Quote:
Also, please edit your code and use code tags and proper indentation. Most of us won't even bother looking at unformatted code. |
|
#5
|
|||
|
|||
|
Re: scoring Students
I think I am doing something wrong in the while loop
|
|
#6
|
|||
|
|||
|
Re: scoring Students
|
|
#7
|
|||
|
|||
|
Re: scoring Students
Quote:
Code:
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
char question[20][5];
int main()
{
ofstream fanswers, fcorrect_keys;
char correct_keys[21];
char student_id[12], answers[5][20];
char id_buf[12], answers_buf[20];
char student_count = 0;
fcorrect_keys.open("Answers.dat");
while(!fcorrect_keys.eof()) {
fcorrect_keys >> answers_buf;
strcpy(correct_keys, answers_buf);
}
// cout << correct_keys << endl;
fcorrect_keys.close();
fanswers.open("Tests.dat");
while(!fanswers.eof()) {
fanswers >> id_buf >> answers_buf;
strcpy(student_id[student_count], id_buf);
strcpy(answers[student_count],answers_buf);
student_count++;
}
// for(int i=0; i < student_count;i++)
// cout << student_id[i] << " " << answers[i] << endl;
fanswers.close();// close the file
cout << " student_id number_correct" << endl << endl;
for(int i = 0; i < student_count; i++) {
int correct_count = 0;
for(int j = 0; j < 20; j++) {
if (answers [i][j] == correct_keys[j])
correct_count++;
question[j][answers[i][j] - 'A']++;
}
printf("%11.11s %2d\n", student_id[i],correct_count);
}
cout << endl << endl << endl << "Number of students taking exam : " << student_count << endl;
printf("\nquestion A B C D E\n");
for(int k = 0; k < 20 ; k++) {
printf(" %2d ",1+k);
for(int l=0;l < 5; l++)
printf(" %2d%c ", question[k][l], (l == (correct_keys[k] - 'A') ? '*' : ' '));
printf("\n");
}
return 0;
}
|
|
#8
|
|||
|
|||
|
Re: scoring Students
It's amazing how hard some people make it to help them.
You've yet to ask a question or say what's happening that you need help with. |
|
#9
|
|||
|
|||
|
Re: scoring Students
Ok .. I am doing the first while loop wrong but I dont know what is wrong I am just asking for a little help for the while loop you dont have to tell me the code..I just need some help with it..And I appreciate your help
|
|
#10
|
|||
|
|||
|
Re: scoring Students
Is "answers.dat" supposed to contain many lines of text? I ask because you have two variables, answers_buf and correct_keys that you read from a file, then write to the variables in a loop until the end of the file, but all this is going to give you is the very last line (you write over it with each iteration of the loop.
|
![]() |
| Bookmarks |
| Tags |
| arrays , strings |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|