-
November 19th, 2009, 11:14 PM
#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;
}
-
November 20th, 2009, 06:33 AM
#2
Re: scoring Students
I need to write a program using arrays and strings.Here is the question.
No, that's you assignment... you didn't ask a question. Maybe you should read this first.
-
November 20th, 2009, 01:16 PM
#3
Re: scoring Students
actually I wrote my code and I know what I did wrong only I dont know how to change it.
-
November 20th, 2009, 01:28 PM
#4
Re: scoring Students
Originally Posted by thebeast91
actually I wrote my code and I know what I did wrong only I dont know how to change it.
Why don't you tell us then so we don't have to guess.
Also, please edit your code and use code tags and proper indentation. Most of us won't even bother looking at unformatted code.
-
November 21st, 2009, 10:47 PM
#5
Re: scoring Students
I think I am doing something wrong in the while loop
-
November 21st, 2009, 11:43 PM
#6
Re: scoring Students
Originally Posted by thebeast91
I think I am doing something wrong in the while loop
http://www.codeguru.com/forum/showpo...35&postcount=4
-
November 22nd, 2009, 11:09 PM
#7
Re: scoring Students
Originally Posted by GCDEF
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;
}
-
November 23rd, 2009, 08:05 AM
#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.
-
November 24th, 2009, 01:11 AM
#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
-
November 24th, 2009, 08:28 AM
#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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|