Runtime error need help

Why is my code giving run time error stack around answer is corrupted? Any idea on how to fix it? Thanks you

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
  #include<iostream>
#include<fstream>
#include<string>
using namespace std;
// prototypes
void getgrad (double[], double [], float &);
bool readarr (double[], ifstream &);
bool readInStudentAnswer(ifstream &, string &, double[]);
int main()
{
	//declaring variables
	ifstream inFile;
	ofstream outFile;
	string StudentID;
	double answer[20];
	double STUDENTANSWERS[20];
	float Scores;

	//opening files
	inFile.open("Answers.txt");
	outFile.open("StudentsScores.txt");

	//function call for reading in answer key
	readarr(answer, inFile);

	// while loop that reads in student ID and calls getgrad function and outputs it into output file
	while (readInStudentAnswer(inFile, StudentID, STUDENTANSWERS))
	{
	
		getgrad(answer, STUDENTANSWERS, Scores);
		cout << StudentID << Scores << endl;
		outFile << StudentID << Scores;
	}


	return 0;
}

// function to read in the answer key
bool readarr(double A[], ifstream & b)
{
	int i;
	for (i = 0; i <= 20; i++)
	{
		b >> A[i];
	}
	return bool(b);

}

//function to read in the student answer
bool readInStudentAnswer(ifstream & c, string & studID, double studANS[])
{
	c >> studID;
	for (int i = 0; i <= 20; i++)
	{
		c >> studANS[i];
	}
	return bool(c);


}

// function to compare student grade to the answer key and get their scores
void getgrad(double B[], double studANS[], float & grade)
{
	float GRADES = 0;
	int i;
	for (i = 0; i < 20; i++)
	{
		if (B[i] == studANS[i])
		{
			GRADES++;
		}
		
	}
	grade = GRADES += 5;
}
1
2
3
4
5
6
7
8
9
bool readarr(double A[], ifstream & b)
{
	int i;
	for (i = 0; i <= 20; i++)
	{
		b >> A[i];
	}
	return bool(b);
}


You are going out of bounds. How big the array?
the array is suppose to be 20
What happens when i = 20 in that loop?
Oh i figured out the error It suppose to be i < 20 correct? Im trying to read in 20 numbers in a file using arrays
Correct. Since arrays are indexed from 0, index 19 is actually the 20th element.

But array[20] is somewhere in memory that doesn't belong to you. You'll get "corrupt memory" errors when you try to write out of bounds.
Thanks so much. SIlly me
Topic archived. No new replies allowed.