What is "Run-Time Check Failure #2"?

The problem happened in line 32.
Whenever it reaches the code "return 0", the following error appear.

Run-Time Check Failure #2 - Stack around the variable 'student' was corrupted


With brief search on google, it is somehow related to putting to many datas into a variable.

I don't know how to solve the problem as I didn't really put to many datas into the struct student.

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
#include <iostream>
#include <string>
using namespace std;

struct record
{
	double quiz1, quiz2, midterm, exam, average;
	string grade;
};

void input(record& student);
void calculate(record& student);
void output(record& student);

int main()
{
	const int class_size = 2;
	record student[class_size];

	for(int i = 1; i <= class_size; i++)
		input(student[i]);

	for(int i = 1; i <= class_size; i++)
	{
		calculate(student[i]);
		output(student[i]);
		cout << endl;
	}

	cout << endl;

	return 0;
}

void input(record& student)
{
	cout << "Please enter the score for quiz 1\n";
	cin >> student.quiz1;

	cout << "Please enter the score for quiz 2\n";
	cin >> student.quiz2;

	cout << "Please enter the score for midterm\n";
	cin >> student.midterm;

	cout << "Please enter the score for exam\n";
	cin >> student.exam;

}

void calculate(record& student)
{
	student.average = ((student.quiz1 * 10  + student.quiz2 * 10) * 0.125 + student.midterm * 0.25 + student.exam * 0.5);

	if(student.average >= 90)
		student.grade = "A";
	if(student.average < 90 && student.average >= 80)
		student.grade = "B";
	if(student.average < 80 && student.average >= 70)
		student.grade = "C";
	if(student.average < 70 && student.average >= 60)
		student.grade = "D";
	if(student.average < 60)
		student.grade = "F";
}

void output(record& student)
{
	cout << "The score for the quizzes are: "
		 << student.quiz1 << " " << student.quiz2 << endl;

	cout << "The score for the midterm is: "
		 << student.midterm << endl;

	cout << "The score for the exam is: "
		 << student.exam << endl;

	cout << "Your average is: "
		 << student.average << endl;

	cout << "Your grade is: "
		 << student.grade << endl;
}
You're stepping out of bounds of your 'students' array.

Remember that array indexes start at zero... not at one. So this:

for(int i = 1; i <= class_size; i++)

Is wrong because you are looping over index [1] and [2].

[2] is an invalid index. Since the size of the array is 2... that means [0] and [1] are valid indexes.

For this reason, the typical for loop starts at zero and counts up to the size (but not = to the size). IE:

for(int i = 0; i < class_size; i++) // <- start at 0. < instead of <=
Topic archived. No new replies allowed.