outputting number of correct answers on a test

closed account (jNA7M4Gy)
I need to write the following program in C++ and I have no idea what to do. I have read my book and looked online and don’t really know what to do. I cannot use arrays either. I was really hoping that someone could help me because that would be great!!!! THANKS!!! Here is the program I need to do:


You’ve been asked to write a program to grade a multiple choice exam. The exam has 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 a string 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 to file scores.dat. Erroneous input should result in an error message.

Given the data for exams.dat:

abcdefcbcdefabcdefab
1234567 abcdefabcdefabcdefab
9876543 abddefbbbdefcbcdefac
5554446 abcdefabcdefabcdef
4445556 abcdefabcdefabcdefabcd
3332221 abcdefghijklmnopqrst


The program would output on scores.dat;

1234567 20
9876543 15
5554446 Too few answers
4445556 Too many answers
3332221 Invalid answers
Ok, I would start by getting the data from the file:

http://www.cplusplus.com/reference/iostream/fstream/

Then you would need to split the strings...use search, there are some other topics that had this type of question.

Then, I would put the it in a vector/array (depending on requirements, use a vector if possible):

http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/doc/tutorial/arrays.html

After that, just iterate through the vector/array and compare the answers. If an answer is too high, then return Invalid answers. If you reach the end of the answers before the end of the test, return Too few, and vice versa, return Too many. Then you can just use fstream again to create/open scores and put the data in.
Otherwise, if you are not familiar with vector etc as suggested above, you can use a single string to read the answers stream and check the length() of it to determine if it has too few or too many characters, like < 20 or > 20 to say too few or too many.

It would look like:

fs >> id >> answerString ;
where id a long integer and answerString is 'string' type.

And for checking each character, you either can use [] or at() function of 'string' object.
Check it out. Good luck :)
Last edited on
The comparator operator is a single < sign, << is bit shifting at best, insertion at worst.
Sorry for the typo. I corrected it above.
I meant it is output (extraction) operator.
Topic archived. No new replies allowed.