I am writing code for a C++ class. I have tried a couple of different ways to write it, but I keep failing. Here is what I have so far and also what the assignment asks us to do. Please help
You have 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 'a' through 'f'. The data are stored in a file called exams.txt where the first line is the key, consisting of a string of 20 characters. The remaining lines in the file are exam answers; each consists of a student ID number, a space, and a string of 20 characters. The program should read the key, read each student ID and exam answers, and output the ID number and the number of correct answers to a file called scores.txt. 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 would output the following data in scores.txt
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 formatted neatly, and the error messages should be informative.
Also, I know this is a stupid question, but I am a newbie. When I am doing in data files and out data files, how do I save my data to those files and have them open when I run my program?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
bool WrongSize (ofstream& outData,int keylength,string answers)
{
bool valid = false;
if (answers.length() < keylength)
{
outData << "Too few answers!\n";
}
else if (answers.length() > keylength )
{
outData << "Too many answers!\n";
}
else
valid = true;
return valid;
}
int main()
{
< snip >
if( WrongSize (outData, keylength, answers) == true )
{
for( int count = 0; count < key.length(); count++)
{
if( answers[count] == key[count] )
grade++;
else
outData << "Invalid answers" ;
}
}
}