Arrays-HW Question need help finding error in code

Hello you might of seen this question before:

The history teacher at your school needs help in grading a True/False test. The students' IDs and test answers are stored in a file. The first entry in the file contains answers to the test in the form:

TFFTFFTTTTFFTFTFTFTT

Every other entry in the file is the student ID, followed by a blank, followed by the student's responses. For example, the entry...

ABC54301 TFTFTFTT TFTFTFFTTFT

indicates that the student ID is ABC54301 and the answer to question 1 is True, the answer to question 2 is False, and so on. This student did not answer question 9. The exam has 20 questions, and the class has more than 150 students. Each correct answer is awarded two points, each wrong answer gets one point deducted, and no answer gets zero points. Write a program that processes the test data. The output should be the student's ID, followed by the answers, followed by the test score, followed by the test grade.
Assume the following grade scale: 90%-100%, A; 80%-89.99%, B; 70%-79.99%, C; 60%-69.99%, D; 0%-59.99%, F.

My 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
  #include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

const int answers = 20;
const int numberofStudents = 150;

using namespace std;

void grading(char studentTest[][answers], string studentID[], char key[]);
char letterGrade(int score);

int main()
{
	string studentID[numberofStudents];
	char key[answers + 1];
	char studentTest[numberofStudents][answers];
	ifstream inFile;
	ofstream outFile;
	char fileName[60];

	cout << "Enter in the input file name: ";
	cin >> fileName;
	
	inFile.open(fileName);
	outFile.open("output.txt");

	if (!inFile)
	{
		cout << "Cannot open the inputfile." << endl;
		return 1;
	}
	
	outFile.open("output.txt");

	inFile.getline(key, '/n');

	for (int i = 0; i < numberofStudents; i++)
	{
		inFile >> studentID[i];
		inFile.get();
		for (int j = 0; j < answers; j++)
			studentTest[i][j] = inFile.get();
	}

	grading(studentTest, studentID, key);

	cin.ignore();
	cin.get();

	return 0;

}

void grading(char studentTest[][answers], string studentID[], char key[])
{
	int totalScore = 0;

	for (int i = 0; i < numberofStudents; i++)
	{
		cout << "Student ID: " << studentID[i] << endl;
		cout << "Answers: ";
		for (int j = 0; j < answers; j++)
		{
			cout << studentTest[i][j];
			if (studentTest[i][j] == key[j])
				totalScore += 2;
			else if (studentTest[i][j] != key[j] && studentTest[i][j] != ' ')
				totalScore -= 1;
		}
		if (totalScore < 0)
			totalScore = 0;

		cout << endl;
		char grade = letterGrade(totalScore);
		cout << "Grade: " << grade << endl;
	}
}

char letterGrade(int score)
{
	double percent = static_cast<double>(score) / 40;
        cout << "Score: " << percent * 100 << "%" << endl;

	if (percent >= 0.9)
		return 'A';
	else if (percent >= 0.8)
		return 'B';
	else if (percent >= 0.7)
		return 'C';
	else if (percent >= 0.6)
		return 'D';
	else
		return 'F';

}


Its running fine and I have solved the file reading issue however my output is all wrong, it is not comparing i am assuming since all the grades for my input file come out as A. And the Score percent comes out as a large number like 3065% or 3047.5% (that is definitely not 90 percent or below)

so a sample of the txt file it is reading would be along the lines of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
TFFTFFTTTTFFTFTFTFTT

KSD54701 TFTFTFTT TFTFTFFTTFT
IOU59310 FTTFTFTTFTTFTTTFTFTT
ADF39453 FTTFTFTTFTFFFTFTTFTF
KIL38893 FTFFFFFFFFFTF TFFTTT
ASC29394 FTTFTFTFFTFTFTTFTTFT
ABC15476 FTFTFFFTFFTFTFTFFTFT
HGF14437 FTFTFF FTFTTTTFFTTFT
BNM38558 FTFTTTTTTTFFFTFTFTFF
CVF10439 FTTTFTFTFTFTFFTFTFFT
AHD17570 FTTFTFFF   TTTFTFTFT
YUH16561 FTFFTFTTFTFTFFFTFTFT
YBA10512 FTFFTTTFTFFFFFTFTFTF


I am assuming i need to fix my arrays in the function somehow, but im not sure where. Also I am guessing it would be better to output the data to another file, however when i changed the couts into outfiles instead that didnt go too well and kept saying outfile was undefined.

Thank you for any help!
Last edited on
You're using the same variable to sum the scores of each student without resetting its value each time through the loop, so the total scores of all the students keep accumulating.
Oh wow I should of noticed that, this is why you take naps. Thank you.
Topic archived. No new replies allowed.