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

C++ You\'ve been asked to write a program to grade a multiple choice exam. The e

ID: 3621632 • Letter: C

Question

C++
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 ; each consists of a student ID number, a space, and a string of 20 characters. the program should read the key, 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 abcdefabcdefabcde
4445556 abcdefabcdefabcdefabced
3332221 abcdefgrhadbhfuejshhh

the program would output the following data 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 comments. Error messages should be informative.


Explanation / Answer

please rate - thanks

#include <iostream>
#include <fstream>
using namespace std;
string getanswers(ifstream&);
int grade(string,string);
void printstudent(int,int,ofstream&);
int main()
{int correct,id;
string answers,student;
ifstream in;
ofstream out;
in.open("exams.dat");       
if(in.fail())           
   { cout<<"input file did not open please check it ";
   system("pause");
   return 1;
   }
out.open("scores.dat");   
answers=getanswers(in);
in>>id;
while(in)
    {student=getanswers(in);
   correct=grade(answers,student);
   printstudent(id,correct,out);
   in>>id;
   }
    
out.close();
in.close();
return 0;
}
string getanswers(ifstream& in)
{string s;
in>>s;
return s;
}
int grade(string a,string s)
{int i=0,tot=0;

for(i=0;s[i]!=''&&i<20;i++)
    if(s[i]<'a'||s[i]>'f')
        return -30;
    else if(s[i]==a[i])
        tot++;
if(i<20)
    return -10;
if(i==20&&s[i]!='')
     return -20;
    
return tot;
}
void printstudent(int i,int s,ofstream& out)
{if(s==-10)
    out<<"student "<<i<<" - too few answers ";
else if(s==-20)
    out<<"student "<<i<<" - too many answers ";
else if(s==-30)   
    out<<"student "<<i<<" - invalid answers ";
else  
    out<<"student "<<i<<" - "<<s<<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