True/False Test Score Calculator

I'm almost finished with this program but something wrong is happening with the calculations. This program reads from an input file, which lists the correct answers for a true/false test, then 150 student's IDs and test answers. The program gets the correct answers and compares it to the student's answers. 2 points are given for a correct answer and 1 point is taken away for an incorrect answer. The points are divided by the max number of points, 40, then multiplied by 100 for a percentage. I can't figure out why all the numbers in the output are wrong...

I left the input file text I'm using at the end of the 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
130
131
132
133
134
135
  #include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>

using namespace std;

void readDataFromFile(ifstream &, char[], string[], string[]);                   // Function Prototypes
void calculateScores(char[], string[], double[]);
void calculatedGrades(double[], char[]);

const int MAX_ANSWERS = 20;                                                      // Global constants/variable
const int MAX_STUDENTS = 200;
const double MAX_MARKS = 40.0;

int studentsCounter = 0;

int main()
{                                                                                // Declare variables
	ifstream inFile;                                                             
	string fileName;                                                             
	char testAnswers[MAX_ANSWERS];                                               
	string studentID[MAX_STUDENTS];                                              
	string studentAnswers[MAX_STUDENTS];                                         
	double studentScores[MAX_STUDENTS];                                          
	char studentGrades[MAX_STUDENTS];                                            
	                                                                             
	cout << "Enter the input file name: ";                                       
	cin >> fileName;                                                            
	                                                                             
	inFile.open(fileName);                                                       
	                                                                             
	if (!inFile)                                                                 // If the file is not found, display error message.
	{                                                                            
		cout << "The file " << fileName                                          
			<< " is not found. " << endl;                                        
		                                                                         
		system("pause");                                                         
		return 1;                                                                
	}                                                                            
    
	readDataFromFile(inFile, testAnswers, studentID, studentAnswers);            // Call Functions to read file & make calculations

	calculateScores(testAnswers, studentAnswers, studentScores);

	calculatedGrades(studentScores, studentGrades);

	cout << "\nResults of the student's tests: " << endl;                        // Fancy format for readability
	cout << "Student_ID\tStudent_Answers\tScore\tGrade" << endl;
	cout << "---------------------------------"
		<< "----------------------------------" << endl;

	for (int student = 0; student < studentsCounter; student++)                  
	{
		cout << studentID[student] << "\t"                                       // Output each student's information
			<< studentAnswers[student] << "\t"                                   // and test scores in columns
			<< studentScores[student] << "\t"
			<< studentGrades[student] << endl;
	}

	inFile.close();                                                              // Close input file
																				 
	system("pause");                                                             
	return 0;	
}                                                                         

void readDataFromFile(ifstream &inFile, char testAnswers[],                      // Function that reads correct answers from
	                  string studentID[], string studentAnswers[])               // first line in the input file, then the
{                                                                                // student's IDs and test answers
	char ch;
	int index;

	for (int i = 0; i < MAX_ANSWERS; i++)
		inFile >> testAnswers[i];

	index = 0;
	inFile >> studentID[index];
	while (inFile)
	{
		studentsCounter++;

		inFile.get(ch);

		getline(inFile, studentAnswers[index]);

		index++;
		inFile >> studentID[index];
	}
}

void calculateScores(char testAnswers[],                                          // Function to calculate the score of
	                  string studentAnswers[],                                    // each student's test based on what
	                   double studentScores[])                                    // answer is correct, incorrect, or incomplete
{
	string answers;
	double sum;

	for (int i = 0; i < studentsCounter; i++)
	{
		answers = studentAnswers[i];
		sum = 0;

		for (int f = 0; f < answers.length(); f++)
		{
			if (answers[f] == testAnswers[f])
				sum = sum + 2;
			else
				sum = sum - 1;
		}
		studentScores[i] = sum;
	}
}

void calculatedGrades(double studentScores[],                                      // Function to calculate what letter grade
	                  char studentGrades[])                                        // a student recieves based on the score
{
	double percentage;

	for (int i = 0; i < studentsCounter; i++)
	{
		percentage = (studentScores[i] / MAX_MARKS) * 100;

		if ((percentage >= 90) && (percentage <= 100))
			studentGrades[i] = 'A';
		else if ((percentage >= 80) && (percentage <= 89.99))
			studentGrades[i] = 'B';
		else if ((percentage >= 70) && (percentage <= 79.99))
			studentGrades[i] = 'C';
		else if ((percentage >= 60) && (percentage <= 69.99))
			studentGrades[i] = 'D';
		else
			studentGrades[i] = 'F';
	}
}
here is the input file.....
TFFTFFTTTTFFTFTFTFTT
Student001 TFTFFTTFTTFFTFFTFFTT
Student002 FTFFFFFTTTTTTTFFFTFT
Student003 FFFTTFTFFTFFFTFTTTFF
Student004 TTTFTTFFFFFTTTFFFFTT
Student005 TFFFTFTFFTFFTTFTTFFF
Student006 TTFTFFFTTTTFTFFTTTTT
Student007 TTTTFTFTTFFFTFFFTTTF
Student008 FFFFFTTFFTTFFTTFFFFF
Student009 TFTFFFFFTTTTFFFFFTFF
Student010 TFFFTTFFFTTTTFTFTTFT
Student011 FTFFTFFTTFFTTFFTTFTT
Student012 TFFTFFTFTTTTFTFTTFFT
Student013 FTFTFFFTFFFFTTFFFTTT
Student014 FTFTFTTFFFTFTFFTTFFF
Student015 FTFFFFFTTFTFTTFTFTFT
Student016 FFTFFTTFFFTTTTFTTFTT
Student017 TTTFFTFTFFFFFTTTTTTT
Student018 TFTTTTTFTTTTFFFTTFFF
Student019 FTFTTFFTFFFTFTTFFFFF
Student020 FTTTFFFTFFFTFTFFTTFF
Student021 TTTFTTFTFFFTTTFTTTTT
Student022 TTTFTTTTTFFFFFTFFFTT
Student023 TTFTFFTFTTTFFFTTFTFT
Student024 TTFTFTFFTFTFTTFTTFTT
Student025 TFTTFTTTTTFTTTTFTTFT
Student026 TTFTTTTFFTTFTTTFTTFT
Student027 TFFFTFTFTFFFFFTFTTFT
Student028 TTFFFTTFFFTFFTFFTFTF
Student029 FTFTTFFFTTFTFFTFTFTF
Student030 FFTTFFTTTFFFFFTFTFTF
Student031 TTFFTFFFFFFFTFFFTTTF
Student032 FTTFTTFTFFTFTTTFTTFT
Student033 TTFTTFTTTTFFFTTTTTFF
Student034 FFTTTFFFFTTFTFFTFTFT
Student035 FFTTFFFFFFFTFTFFTTFT
Student036 FFTTFTFFFTFTTFTFTTTF
Student037 FFTTTTTTFTTTTFTTTFTT
Student038 FFTFFFTFTFFFFFFTTFFF
Student039 FFFTTFFTFFFFFTFFFFFT
Student040 FTFFFTTFTTTFTFTFFTFT
Student041 FFFTFFFTFFFFTFTFTFTF
Student042 TFTTTTFFFTFTTFFFTTTT
Student043 FTTFTFFTFTTFTFFTFFFF
Student044 FFFFTFTFTTTFFTFTFFFF
Student045 FFFFTTTFFFFTTTTTFTFF
Student046 FTTFFTTFTFTTFTTTTTTT
Student047 TTFTTFTTFTTFFFTTFTTT
Student048 FFFFFFTFTTFFTTTFTTTT
Student049 FFTTFFFTTTTTTFTFTFTT
Student050 TTFFFFTTTFFTFFTFFTTF
Student051 TFFTFTTTFTFTTFFFTTTT
Student052 TFFTTFFTFTTTFFTTTTFT
Student053 TTTFFFFTFTTTTFTFFTTT
Student054 TFTTFTFFFFTFTFFTFTTT
Student055 TFFFTFFTTTTFTTFTTFFT
Student056 TTTTFTFTTFFFFFTFFFTF
Student057 FFTTFTTTTFTFFTTTFFFF
Student058 TTFTTTTTFFTFTFTTFTTT
Student059 FTFTFFFFFTTTFFFTFTTF
Student060 FTTFFFFFTFFFFTTFTFFF
Student061 FTFTFFTTTTTFTFFFFFFT
Student062 FFTTTFTTFFFTTTFFTTFT
Student063 TFFTTTTFTFTTFTTFFFTF
Student064 FFFFTTFTFTFFFFFFFFTT
Student065 FTFFTTTFTFTTFFTTTTFT
Student066 TTTTFTTFFTTFTTFFFTFF
Student067 TTFFTFTFTFTFFTFFTFFT
Student068 TTFFTTTFTFTTTTFTTTTF
Student069 TFFFTFTTTTFFFFFFFFTT
Student070 FFTTTFFTTFFFTTFFFFFT
Student071 FFTFTFFFTTTFTTFFTFFT
Student072 FTFFTFFTTTFTFFTTTFTF
Student073 FFTFFFTTFFTFFFFFFTTF
Student074 FFFFTTTTFTFTFTFTTTFT
Student075 TTFFFTFTTFFFTTFFFTTT
Student076 TTFFTFFTFFTTFTFFFTTT
Student077 FFFTFFFTTTFFFFFFFTTT
Student078 FTTTFFFFFFTFFTFTFFTT
Student079 TFFTTFTTTFTFTTTFTFFT
Student080 FFTFTTFTTFTTTFTTTTFT
Student081 FTFFTTTFTFTFTFFFTTTF
Student082 TTTFTTFFFTTFTFTFFTTF
Student083 FFTTTTTTFFFFFFFTTTFF
Student084 TFTFFTTTFTFTFFTTTTTT
Student085 FFFFFFTTFTTFFFFTTTTT
Student086 TTFTTTTFFFFTFTFTTFTF
Student087 TTTTFTFFFTTTTFFTFTFT
Student088 FTTFFTTTTTTFFTTTTTFF
Student089 FFFFTTFTTTTTTFFFTTFF
Student090 TFFTFFTTTTTFTFTFFTFF
Student091 TFTFTTFFFTTTTTFTTFFF
Student092 TFTFTTTFFTFTTTTTFFTF
Student093 TTTFTFFTTFTFFFTTFFTF
Student094 FFFFTFTTTFFTFFTTTTTF
Student095 FFTFFTTTFTTTFTTFTTFF
Student096 TFFFTFFFTTFFFFTTFFFT
Student097 TFFFFTFFFFTFFFTTFFTF
Student098 TTFFTTFTFTFFTFFTFTTF
Student099 TTTFTFFTTTTTTTFTTTTF
Student100 TFFFFFFTTTTTFFTFFFTF
Student101 FFTFTTTTTTFTTTTFTFFF
Student102 TTFFFTFFTTTTFTFFTTFT
Student103 TTFTTTFFFFTTTFFTTTTF
Student104 FTFTTTFFFFFTFTFTFTFT
Student105 FFFTFFFFTTTTTFTFTTFF
Student106 FFTFTFTFFTTFFFFFFTTF
Student107 FTTTFTTTFTFFFTFTFFFF
Student108 FTFTFTFTFFTFFTTTFTTT
Student109 FTFFTFFFTTFTFTFTFFFT
Student110 TTTTTFFFTFFFTFTFFFFF
Student111 TFFFTFFTTTTFFTTFTFTT
Student112 FFFTTFFFFTTFTTFFTTTT
Student113 FFFTFFTTFTTFFFFTTFTF
Student114 TTTFTFTTFFFFTFFTTFFF
Student115 TFTTTFFFTFTFFFTFFFTF
Student116 FTFFTTFFFFFFFTFFTFTT
Student117 FFFFTFTTFTTFTFFFFTTT
Student118 TFTFFFTTFFTFTTTTTTFF
Student119 TFTTTFTTFTFTFFTFFFFF
Student120 FTTFFTFFFTFFTTFFFTTT
Student121 TTFFTTTTFTFFFFFFTFFF
Student122 TFFFFFTTTTTTTTFFFTTF
Student123 FFFFFTFFTFFTTFTFFFFT
Student124 FTFFFFFFTFFTTTTTFFFT
Student125 TFTTFTFFTTFTTFTFTFTT
Student126 FTFTTFFTFTFFTTFTTTFT
Student127 FFTTFFTTFFFFTFFTTFFF
Student128 FTTTFTTFFFTFTTFFTTTT
Student129 FFFTTFTTTTFTFFTFFFTF
Student130 FTFTTFTTFTFFFFFFFTFT
Student131 FTTTFFTFTFFFTTTTTFTF
Student132 TFTFFTTTTTFFFTFTFFFF
Student133 TFTTFTTTTTTFFFTTFTFF
Student134 TTTTTTTFFTTTFTFTFTFT
Student135 TTFFTTFTFTTTFTFTTTTF
Student136 TTTFFFFTFTTFFTTFTTTF
Student137 FTTFFFTFTFTTTFFTTFFF
Student138 TFTTTTFTFFTFTTFTFFTT
Student139 TTFFTTTFTFFFTFFFFTFT
Student140 FFTTFFTFFTFTFFTFFFFF
Student141 TTTFTTTFFFFTTFFTTFFF
Student142 TFTFTTTTTTFFTFTTFFFF
Student143 TTTFTTFTTTTFTTTTFTTF
Student144 TFTFTFTTFTFFTFTFFTFF
Student145 FFFTTFTFFFTFFFFTTTTF
Student146 FTTFFTFFFFFTFTFTTFFF
Student147 FFFTTFFFTTFTFFFTFFFF
Student148 FTTFTFTFTFFFTTFTTFTT
Student149 TTTTFFFFTTTTFFTTFFTT
Student150 TTFFFFTTFFFTTTTTTFFF
something wrong is happening with the calculations
I can't figure out why all the numbers in the output are wrong

What does "wrong" mean?
Why do you think they are "wrong"?
In what way are they "wrong"?

For the first two students, show what you think the "right" output would be and also show the "wrong" output you are getting.
I think I see what is happening. I guess my professor just randomized all the T's and F's for the student's answers so the majority of the grades are F's. when the output showed the first three student's scores as 19, 7, and 7, I thought that was the percentage. which would be 47.5%, 17.5%, 17.5%
so, nothing wrong was happening, sorry!
But you still helped, you made me look further into it :)
Yeah, he must've generated the student answers randomly.
That's not a good method for this.
Here's a way to generate a more interesting input file.

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
#include <iostream>
#include <iomanip>
#include <random>
#include <algorithm>

const int NumMarks = 20;
const int NumStudents = 150;

int main()
{
    std::default_random_engine rnd {std::random_device{}()};
    std::uniform_int_distribution<> dist(0, 20);

    char answers[NumMarks+1] = "TFFTFFTTTTFFTFTFTFTT";

    std::cout << answers << '\n';
    std::cout << std::setfill('0');

    for (int i = 0; i < NumStudents; ++i)
    {
        int n = dist(rnd);                          // n = 0 to 20
        bool correct[NumMarks] {};                  // start all false
        while (n-- > 0) a[n] = true;                // set n of them to true
        std::shuffle(correct, correct + NumMarks, rnd); // and shuffle them

        std::cout << "student" << std::setw(3) << i + 1 << ' ';
        for (int i = 0; i < NumMarks; ++i)
            if (correct[i])  // correct answer
                std::cout << answers[i];
            else             // incorrect answer
                std::cout << (answers[i] == 'T' ? 'F' : 'T');
        std::cout << '\n';       
    }
}

Partial example output:

TFFTFFTTTTFFTFTFTFTT
student001 TFFFTTTFTFFFFFFFTFTT
student002 TTFTFFTTFTFFTFTFTFTT
student003 FTTFTTFFFFTTFFFTFTFF
student004 FTTTTFTFFFTTFFFFFTFF
student005 TTFTFTTTTTFTTFTFTFTF
student006 TTFFFTTFFFFFFTTFFTFF
student007 FTTFTTFFFFTTFTFTFTFF
student008 FTTFTTFFFTTTFTFTTTTF
student009 FFTFTTTTFTTTFTTFFTFF
student010 FTTFTTFFFFTTFTFTFTFF
student011 FTTFTTFFFFTTFFFTTTFF
student012 TTTFTFFFTTTTTTFTFTTF
student013 TFFTFFFTTTTFTFTFTFTT
student014 FTTFFTTTFFTTTFFFFFFF
student015 TFFTFFTTTTFFTFTTTFTT
student016 TTTTTTFTFTTFFFFFFFTT
student017 FTTFFFTTFFTFFTTTTTFT
student018 TTFTFFTFTTTFTFTFFFFT
student019 FFTFTTFFFFTTFTFTFTFF
student020 FTFTFTTTFTTFFTTFTFFF
wow, :O

thats cool haha
Topic archived. No new replies allowed.