Multiple-Choice Program

I have to write a multiple choice program and the following code is as far as i've gotten. It's coming back with invalid answers and missing information and I'm not sure where i'm doing it wrong.


You’ve been asked to write a program to grade a multiple choice exam. The exam has 20 questions, each answered with a little 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 files 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. For example, given the data:

abcdefabcdefabcdefab
1234567 abcdefabcdefabcdefab
9876543 abddefbbbdefcbcdefac
5554446 abcdefabcdefabcdef
4445556 abcdefabcdefabcdefabcd
3332221 abcdefghijklmnopqrst

The program should output on scores.dat:

1234567 20
9876543 15
5554446 Too few answers
4445556 Too many answers
3332221 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,




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void WrongSize (ofstream&, int, string);

int main()
{
	ifstream inData;
	ofstream outData;
	inData.open("exams.dat");
	outData.open("scores.dat");

	string key;
	string student;
	string answers;
	int keylength;
	int grade;
	grade = 0;


	inData >> key;
	keylength = key.length();

	while (!inData.eof())
{
	inData >> student;
	outData << student<< " ";
	inData >> answers;
	WrongSize (outData, keylength, answers);

	}
for( int count = 0; count < key.length(); count++)
{
   if( answers[count] == key[count] )
	   grade++;
   else
	   outData << "Invalid answers" ;
}


return 0;
}


void WrongSize (ofstream& outData,int keylength,string answers)
{
if (answers.length() < keylength)
{
outData << "Too few answers!" << endl;
}
else if (answers.length() > keylength )
{
outData << "Too many answers!" << endl;
}
else if (answers.length() == keylength)
{
return;
}
}


Well what is the actual output? You didn't give us much more info other than the requirements and the program.

First I think that you have this problem. I think that your first while loop will result in one set of bad file reads.
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5

What did you do to debug the program? Take a look at the entire FAQ section 15.1-15.7. I think that you need to add error checking to the I/O operations in order to meet the requirements.
Invalid answers are not the same thing as incorrect answers. Invalid answers are those that are not in the set a..f. You can see if there are invalid characters using find_first_not_of() .
This is the output:

1234567 9876543 5554446 Too few answers!
4445556 Too many answers!
3332221 3332221 Invalid answersInvalid answersInvalid answersInvalid answersInvalid answersInvalid answersInvalid answersInvalid answersInvalid answersInvalid answersInvalid answersInvalid answersInvalid answersInvalid answers


But should look like this:

1234567 20
9876543 15
5554446 Too few answers
4445556 Too many answers
3332221 Invalid answers


I think I have an error with my eof function that I'm using but I looked at that link recommended and that is for functions that we haven't covered in class so I don't think that would be the proper cpramming lingo to use..does that make sense? Same with your post PanGalactic.
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void WrongSize (ofstream&, int, string, string);

int main()
{
ifstream inData;
ofstream outData;
inData.open("exam.dat");
outData.open("scores.dat");

string key;
string student;
string answers;
int keylength;

inData >> key;
keylength = key.length();

do
{
inData >> student;
outData << student<< " ";
cout << student<< " ";
inData >> answers;
WrongSize (outData, keylength, answers, key);

}
while (!inData.eof());

cout << " lol";
return 0;
}


void WrongSize (ofstream& outData, int keylength, string answers, string key)
{
int grade = 0;
if (answers.length() < keylength)
{
outData << "Too few answers!";
cout << "Too few answers!";
}
else if (answers.length() > keylength )
{
outData << "Too many answers!";
cout << "Too many answers!";
}
else
{
bool check=false;

for (int count = 0; count < key.length(); count++)
{
if( answers[count] == key[count] )
grade++;
else if (answers[count] != 'a' || answers[count] != 'b' || answers[count] != 'c' || answers[count] != 'd' || answers[count] != 'e' || answers[count] != 'f')
check=true;
}

if (check==true)
{
outData << "Invalid Answer!";
cout << "Invalid Answer!";
}
else
{
outData << grade;
cout << grade;
}
}
outData << endl;
cout << endl;
}
This only returns 0 and there are a few things in there that are not supposed to be..for example:

1
2
3
4
5
{while (!inData.eof());

cout << " lol";
return 0;
}


I tried to manipulate what you provided and still couldn't come up with a proper program. ......
/*
sori..i just kidding with lol..

the real answer is
*/
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void WrongSize (ofstream&, int, string, string);

int main()
{
ifstream inData;
ofstream outData;
inData.open("exam.dat");
outData.open("scores.dat");

string key;
string student;
string answers;
int keylength;

inData >> key;
keylength = key.length();

do
{
inData >> student;
outData << student<< " ";
cout << student<< " ";
inData >> answers;
WrongSize (outData, keylength, answers, key);

}
while (!inData.eof());

return 0;
}


void WrongSize (ofstream& outData, int keylength, string answers, string key)
{

int grade = 0;
if (answers.length() < keylength)
{
outData << "Too few answers!";
cout << "Too few answers!";
}
else if (answers.length() > keylength )
{
outData << "Too many answers!";
cout << "Too many answers!";
}
else
{
bool check=false;

for (int count = 0; count < key.length(); count++)
{
if( answers[count] == key[count] )
grade++;
else if (answers[count] != 'a' && answers[count] != 'b' && answers[count] != 'c' && answers[count] != 'd' && answers[count] != 'e' && answers[count] != 'f')
check=true;
}

if (check==true)
{
outData << "Invalid Answer!";
cout << "Invalid Answer!";
}
else
{
outData << grade;
cout << grade;
}
}
outData << endl;
cout << endl;
}
This code doesn't work..it returns 0's......is it because of the eof loop?
Hi there,
Did anyone find the final solution for this question?
I don't Know how to do it.
read the post by pangalactic and I. I'm not going to waste my time when the OP is going to reject the answers for asanine reasons. The OP Is obviously using fstream therefore you have to know how to use a stream correctly in order to read input in the first place. Nothing that we suggested as a solution is advanced nor complex.
Topic archived. No new replies allowed.