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 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,
#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;
inData >> key;
keylength = key.length();
while (!inData.eof())
{
inData >> student;
outData << student<< " ";
inData >> answers;
WrongSize (outData, keylength, answers);
if (answers.length() == keylength)
{
int count;
count = 1;
while (count <= keylength)
{
int grade;
string answer;
string letter;
grade = 0;
letter = key.substr(count, 1);
answer = answers.substr(count, 1);
if (answer < "a" || answer > "f")
{
outData << "Invalid answers!" << endl;
}
else if (letter == answer)
{
grade++;
count++;
}
}
}
}
return 0;
}
void sWrongSize (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;
}
}