Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello I was wondering if someone would be able to help me write my code. This is

ID: 3640256 • Letter: H

Question

Hello I was wondering if someone would be able to help me write my code.
This is the problem...

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

This is what I have so far...

#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;
}

I would really appreciate if someone could help me out!!

Explanation / Answer

your program outputs the right answers so where do you need help? The only error is that you forgot to close outputData....

#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");
   if (!inData) //check if the program successfully open input file. If not, terminate the program
   {
      cout << "Cannot open input file. Program aborting ";
      exit(1);
   }
   outData.open("scores.dat");
   if (!outData) //check if the program successfully open output file. If not, terminate the program
   {
      cout << "Cannot open output file. Program aborting ";
      exit(1);
   }

   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());

   inData.close(); //close input file stream
   outData.close(); //close output file stream
   cin.get();
   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 isInvalid = false; //given a more meaning full name such as isInvalid
      for (int count = 0; count < keylength; count++) //change to given keylength
      {
         if( answers[count] == key[count] )
            grade++;
         else if (answers[count] < 'a' || answers[count] > 'f') //shorter way to check invalid answer
         {
            isInvalid = true;
            break; //terminate the loop if found one invalid answer
         }
      }
      if (isInvalid)
      {
         outData << "Invalid Answer!";
         cout << "Invalid Answer!";
      }
      else
      {
         outData << grade;
         cout << grade;
      }
   }
   outData << endl;
   cout << endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote