grade multiple-choice exam

T

tea-jay

hello every body
our teacher asks us to write this assiment
it has 2 question

i did write the first one but the other was really complicated 2 me
coz
our teacher doesn't know how to explain this
so
plz plz try to solve this problem


You have been asked to write a program to grade a multiple-choice
exam. The exam is out of 20 questions, each answered with a letter in
the range of 'a' through 'f'. The data are stored on a file exams.dat
where the first line is the key, consisting of 20 characters. The
remaining lines on the file are exam answers, and consist of a student
ID number, a space, and a string of 20 characters. The program should
read the key, then read each exam and output the ID number and score
the file scores.dat. Erroneous input should result in an error
message. For example, given the data:

abcdefabcdefabcdefab
2001321 abcdefabcdefabcdefab
2001321 aacdffabadefcbcdafab
2001321 abcdefefabcdefab
2001321 afbcddefabcdcefabcdbefab
2001321 abjdefiabcdabgcdxeab

The program would output on scores.dat:

2001321 20
2001321 15
2001321 Too few answers
2001321 Too many answers
2001321 Invalid answers

Use functional decomposition to solve the problem and code the
solution using functions as appropriate. Be sure to use proper
formatting and appropriate comments in your code. The output should be
neatly formatted, and the error messages should be informative.
 
V

Victor Bazarov

tea-jay said:
hello every body
our teacher asks us to write this assiment
it has 2 question

i did write the first one but the other was really complicated 2 me
coz
our teacher doesn't know how to explain this
so
plz plz try to solve this problem

I think FAQ 5.2 covers this one...

V
 
O

osmium

tea-jay said:
our teacher asks us to write this assiment
it has 2 question

i did write the first one but the other was really complicated 2 me
coz
our teacher doesn't know how to explain this
so
plz plz try to solve this problem



You have been asked to write a program to grade a multiple-choice
exam. The exam is out of 20 questions, each answered with a letter in
the range of 'a' through 'f'. The data are stored on a file exams.dat
where the first line is the key, consisting of 20 characters. The
remaining lines on the file are exam answers, and consist of a student
ID number, a space, and a string of 20 characters. The program should
read the key, then read each exam and output the ID number and score
the file scores.dat. Erroneous input should result in an error
message. For example, given the data:

abcdefabcdefabcdefab
2001321 abcdefabcdefabcdefab
2001321 aacdffabadefcbcdafab
2001321 abcdefefabcdefab
2001321 afbcddefabcdcefabcdbefab
2001321 abjdefiabcdabgcdxeab

The program would output on scores.dat:

2001321 20
2001321 15
2001321 Too few answers
2001321 Too many answers
2001321 Invalid answers

Use functional decomposition to solve the problem and code the
solution using functions as appropriate. Be sure to use proper
formatting and appropriate comments in your code. The output should be
neatly formatted, and the error messages should be informative.

It will be easier to debug if you print results instead of writing them to a
file. When you get it working, modify the print stuff as necessary

Think of three functions

main
open the files
read the key
read lines and look for EOF
for each line
print the student id
call valid
if not valid print a message and continue
call grade
print the grade
done
-------------
bool valid(string line)
----------
int grade(string key, string line)

read the file with the getline function, read each *entire* line into a C++
string.
This applies to the key as well as to the data lines.

If you can post code up to the point where you print (or try to print) the
student id you are likely to get further help. OTOH if you get that far you
may find you do not *need* further help.

If the instructor has been talking a lot about STL, he would expect you to
use iterators quite a bit; but I think it would be easier without iterators.
 
Z

zeppe

Victor said:
I think FAQ 5.2 covers this one...

there isn't anything about the need to write in English?

Anyway, for tea-jay:
if the teacher is not able to explain you something, please post here
your doubts.

Regards,

Zeppe
 
T

tea-jay

this is my code hope any one can fixed to me
coz it didn't work in right way


#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void score ( string answer, string str );


fstream infile;
ofstream outfile;

int main()
{

infile.open("exams.txt");
outfile.open("scores.txt");



string answer;
string str;
string id;

infile>>str;

for(int i=1;i<=5;i++)
{

getline(infile,str);
getline(infile,answer);
getline(infile,id);

score (answer,str );
}



infile.close();
outfile.close();





return 0;
}
void score ( string answer, string str )

{

int sum=0;
string id;


for(int i=1;i<=5;i++)
{

if( answer==str)
outfile<<id<<" "<<"20";

else
sum=sum+i;
outfile<<id<<" "<<sum;
{

if( answer>str)

outfile<<id<<" "<<"Too many answers";


else if
(answer<str)

outfile<<id<<" "<<"Too few answers";



else

outfile<<" "<<"Invalid answers";

}



}


}
 
O

osmium

tea-jay said:
this is my code hope any one can fixed to me
coz it didn't work in right way

I'll offer a few random comments, nothing close to a total fix, though.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void score ( string answer, string str );


fstream infile;
ofstream outfile;

int main()
{

infile.open("exams.txt");

See if the open was successful
outfile.open("scores.txt");

Same as above
string answer;
string str;

Is str the most descriptive name you can think of? Does the str represent
anything? Give it that name. If it is the key, call it a key, str is just
too generic in this instance.
string id;

infile>>str;

for(int i=1;i<=5;i++)

Why 5? There are not 5 students. 5 is an *example* in the file the
instructor gave you. You simply must detect EOF to do this right. The
getline function will help you detect EOF; don't ignore the value returned.
{

getline(infile,str);
getline(infile,answer);
getline(infile,id);

The id is not on a line by itself. Look at the sample you posted.
score (answer,str );

This is a bit picky, but some think that I/O should not be offloaded to a
called funstion.
}



infile.close();
outfile.close();





return 0;
}

This isn't going to work but the first step is to get a skelton program that
gives decent parameters to the score function. One that can call score
exactly once for each student represented in the input file.
void score ( string answer, string str )

{

int sum=0;
string id;


for(int i=1;i<=5;i++)

score should compute the score for a single student, not all the students
{

if( answer==str)
outfile<<id<<" "<<"20";

Did you see the message I posted about printing as opposed to writing to a
file??????
 
J

Jerry Coffin

[ ... ]
for(int i=1;i<=5;i++)
{

getline(infile,str);
getline(infile,answer);
getline(infile,id);

At least as I read it, this simply doesn't correspond to the file format
you posted. That file format had the answer key on one line, followed by
five lines, each of which contained a student ID followed by that
student's answers.

For that format, you'd read the data something like:

read answer key
for number of proposed answers {
read ID
read proposed answer
score proposed answer against key
}

Also, unless memory fails, the student ID and that student's proposed
answers were on the same line, so you wouldn't normally want to use
readline to read them (or at least not the ID).
 
O

osmium

:

Also, unless memory fails, the student ID and that student's proposed
answers were on the same line, so you wouldn't normally want to use
readline to read them (or at least not the ID).

I thought getline was a good choice for reading this line, too, it
demonstrates the nice substring capabilities available in <string>.

Something like this

while(getline(inf, line) ) // inf is a file that has been
// opened for reading
{
string id(line, 0, 7);
cout << id; // note no endl
string answers(line, 8, 100);
int code = validate(answers); // code of 0 - input
//is fine to continue to score
int sc = score(answers);
}
// do EOF stuff

where the results returned by both validate and score() are ignored, to
avoid clutter.
In my earlier post to the OP, I think I suggested validate returns a bool,
the above changes that to an int so that printing can be in main, rather
than farmed out to the functions - this strikes me as "more pure". The above
is intended to pay attention to the instructors desire for "functional
decomposition". .

In testing the snippet above, I belatedly noted that the student ids in the
test file are all the same :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top