I keep getting a run time error but the code compiles and opens??

i dont understand

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
  //help from Chegg tutors
//Ayila Reese
#include <iostream>
using namespace std;

char scoreToGrade(double score) {
	char grade = 'F';
	if (score >= 90.0) {
		grade = 'A';
	}
	else if (score >= 80.0) {
		grade = 'B';
	}
	else if (score >= 70.0) {
		grade = 'C';
	}
	else if (score >= 60.0) {
		grade = 'D';
	}
	return grade;
}

int main(int argc, char** argv) {

	double total_score = 0;
	double lowest_score = DBL_MAX;
	double highest_score = DBL_MIN;
	double score;
	double scores[5];
	int report[5];
	char letters[] = { 'A', 'B', 'C', 'D', 'F' };

	for (int i = 0; i < 5; i++) {
		report[i] = 0;
	}

	cout << "Please enter your scores for the following 5 homework assignments:\n";
	for (int i = 0; i < 5; i++) {
		cout << "HW" << i + 1 << ": ";
		cin >> score;
		if (score < lowest_score) {
			lowest_score = score;
		}
		if (score > highest_score) {
			highest_score = score;
		}
		total_score += score;
		scores[i] = score;

		report[scoreToGrade(score) - 'A'] += 1;
	}

	double average = total_score / 5.0;


	cout << "Your average score is " << scoreToGrade(average)
		<< " which is a "
		<< scoreToGrade(average) << ".\n";
	cout << "Your letter grade report is : \n";
	for (int i = 0; i < 5; i++) {
		cout << report[i] << " " << letters[i] << "\n";
	}
	cin.get();
	return 0;
}
because of this;
report[scoreToGrade(score) - 'A'] += 1;
a grade of 'F' will generate a subscript error as indices are 0..4 but 'F'-'A' = 5.


Registered users can post here. Sign in or register to post.