Help with my output

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;
	}
Last edited on
What makes you think you are going wrong?

EDIT: though one mistake that I do see is that you are using = (assignment) in your if statements, when you probably meant to use == (comparison).
Last edited on
My output should be the number correct at the end of the statement. I don't know why it's not. I'm stumped. My output is supposed to look like this:


*********************************************************
The purpose of this program is to grade an Exam.
*********************************************************
Enter input file name: data5.1
Thomas abcdeabcdeabcdeabcdeabcde 25
John abcdecabcdecabcdecababcde 11
Steven aaaaaaaaaaaaaaaaaaaaaaaaa 5
Alice abcdecabcdecabcdecaaabcde 11
Justin abcdedabcdedabcded Too few answers
Jennifer abcdedabcdedabcdedabbbabcde Too many answers
Chris abcdefabcdefabcdxxababcde Invalid answers 10
There are 7 student records
Last edited on
My output should be the number correct at the end of the statement. I don't know why it's not.


It's not? Looks like it is to me.

Your output
Chris abcdefabcdefabcdxxababcde 0


That '0' at the end isn't the number correct?
No.

Its supposed to be the number of correct answers they had. Its compared to the key.
Shouldn't line 73 be:

numStudentRight = GradeMCExam (student key, answers);
I changed that to try it but it output Invalid Answers at the end of each line. Instead of the score.
That brings to light another problem.

= is assignment. == is comparison.

You are using assignment in the if/else conditions in InvalidAnswers, which means the only way that function can return true in its current incarnation is if it is fed an empty string.
Correct. I just don't know why I'm not getting my correct number to output. It's supposed to be the number right that matched the key.
Topic archived. No new replies allowed.