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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
// date Written: 09/November/2013
// I have read and understand the lab submittal policy document.
//♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎♎
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
// Prototypes
void ID_ANSWERS (ifstream&, ofstream&, string&, string&);
void TEST_RESULTS (ofstream&, int, string, string, string);
int main()
{
// Definitions
string Key,
ID,
Answers;
int KeyLength;
ifstream infile;
ofstream outfile;
char exit_char;
// File Test
infile.open("exams.dat");
if (!infile) // Checks to varify if the input file is valid
{
cout << endl << " *** Error: Can not open the input file ***"
<< endl << endl << endl;
cout << "Enter any key to end execution of this program . . . ";
cin >> exit_char;
return 1;
}
outfile.open("scores.dat");
if (!outfile) // Checks to varify if the output file is valid
{
cout << endl << " *** Error: Can not find the output file ***"
<< endl << endl << endl;
cout << "Enter any key to end execution of this program . . . ";
cin >> exit_char;
return 1;
}
// Program
cout << "Grading Program" << endl << endl << endl; // Title
getline(infile, Key);
KeyLength = Key.length();
cout << Key;
ID_ANSWERS(infile, outfile, ID, Answers);
while(!infile.fail())
{
TEST_RESULTS (outfile, KeyLength, Key, Answers, ID);
ID_ANSWERS(infile, outfile, ID, Answers);
}
// Exit
cout << endl << endl << endl;
cout << "Press any key then enter to exit ";
cin >> exit_char;
return 0;
}
// Functions
void ID_ANSWERS (ifstream& infile, ofstream& outfile, string &ID, string &Answers)
// Pre-
// Post-
{
getline(infile, ID, ' ');
getline(infile, Answers);
}
void TEST_RESULTS (ofstream& outfile, int KeyLength, string Key, string Answers, string ID)
// Pre-
// Post-
{
int grade = 0;
outfile << ID << ' ';
if (Answers.length() < KeyLength)
{
outfile << "Too few answers";
}
else if (Answers.length() > KeyLength )
{
outfile << "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)
{
outfile << "Invalid Answer";
}
else
{
outfile << grade;
}
}
outfile << endl;
}
|