I need this program to out put the correct number of answers each person had. I'm unsure where I'm going wrong. Any help would be great!
My output is:
*********************************************************
The purpose of this program is to grade an Exam.
*********************************************************
Enter input file name: data5.1
Thomas abcdeabcdeabcdeabcdeabcde 0
John abcdecabcdecabcdecababcde 0
Steven aaaaaaaaaaaaaaaaaaaaaaaaa 1
Alice abcdecabcdecabcdecaaabcde 1
Justin abcdedabcdedabcded Too few answers
Jennifer abcdedabcdedabcdedabbbabcde Too many answers
Chris abcdefabcdefabcdxxababcde 0
There are 7 student records
Here is my source code.
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 119 120 121 122 123 124 125 126 127 128 129
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
int GradeMCExam (string, string);
void PrintPurpose ();
void PrintGrade(string name, int score);
bool InvalidAnswers (string answers);
int main ()
{
// variable declarations
string key;
string student;
string answers;
int score;
//float keylength;
int anslength;
int count;
count = 0;
int numberStudents = 0;
int studentAnsL = answers.length ();
int numRight = 0;
void PrintPurpose ();
{
cout << " ********************************************************* " << endl;
cout << " The purpose of this program is to grade an Exam. " << endl;
cout << " ********************************************************* " << endl;
}
ifstream inFile;
string fileName;
cout << "Enter input file name: ";
cin >>fileName;
inFile.open( fileName.c_str());
if (!inFile)
{
cout << "Error: Open file failed for " << fileName << endl;
return 0;
}
inFile >> key;
int numStudentRight;
int numberRight;
int keylength = key.length();
inFile >> student;
inFile >> answers;
while (inFile)
{
count++;
// inFile >> student >> answers;
// score = GradeMCExam (student, answers);
cout << left << setw(16) << student << " " << left << setw(30) << answers;
int studentAnsL = answers.length ();
if ( keylength < studentAnsL)
{
cout << "Too many answers" << endl;
}
else if ( keylength > studentAnsL)
{
cout << "Too few answers" << endl;
}
else
{
numStudentRight = GradeMCExam (student, answers);
cout << numStudentRight << endl;
}
inFile >> student >> answers;
}
cout << "There are" << " " << count << " "<< "student records" << endl;
inFile.close ( );
return 0;
}
int GradeMCExam (string key, string answers)
{
int keylength = key.length();
int score = 0;
int count = 0;
int studentAnsL = answers.length ();
int numStudentRight = 0;
keylength = key.length();
for (int x = 0; x < studentAnsL; x++)
if ( answers [x] == key [x] )
{
numStudentRight++;
}
if ( InvalidAnswers (answers))
cout << "Invalid Answers" << endl;
return numStudentRight;
}
bool InvalidAnswers (string answers)
{
int countAnswer = answers.length ();
bool invalid = true;
for (int i = 0; i < countAnswer; i++)
{
if ( answers [i] = 'a')
invalid = false;
else if ( answers [i] = 'b')
invalid = false;
else if ( answers [i] = 'c')
invalid = false;
else if ( answers [i] = 'd')
invalid = false;
else if( answers [i] = 'e')
invalid = false;
else invalid = true;
}
return invalid;
}
|