Getting an exception thrown at me...

Okay, so this is my first time ever using the forums on the c++ community, and I'm seriously hoping somebody can help me with the problem I'm having. I have a homework assignment where I'm supposed to read from a file of students and grades, and output special id codes for each student, and their grades with letter equivalents based on a grading curve.

I have been at this for 8 hours trying to solve a single debugging issue. The debugger is not pointing to anything in the code, but rather popping up an x in white space and giving me this message:

"Unhandled exception at 0x0FA95139 (vcruntime140d.dll) in Grades.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC."

The relevant code around there, to the best of my knowledge, is this section:

int loopct = 0;
while (!fin.fail())
{
loopct++;
fin >> id;
fin >> lastname;
fin >> firstname;
fin >> major;
fin >> year;
fin >> midterm;
fin >> final;
midterms[loopct - 1] = midterm;
finals[loopct - 1] = final;
idcode = code(id, lastname, firstname);
codes[loopct - 1] = idcode;
}

and of course my called function, code, which is:

string code(string id, string lastname, string firstname)
// PRE: id is seven digit identification
// POST: return code of first letter of last name, first letter of first name, last two digits of id
{
char firstletterfirst = firstname[0];
char firstletterlast = lastname[0];
char sixthdigitid = id[5];
char seventhdigitid = id[6];
string idcode = { firstletterfirst, firstletterlast, sixthdigitid, seventhdigitid };
return idcode;
}

Sorry about any text wrapping. I hope this is enough to help somebody help me. :( I'm so confused. I'm gonna go get some coffee and check back in a little bit.


(Before anyone tells me, yes I know this post is a duplicate of another post, also by me, in a different forum. I figured two hooks in the water might catch a fish where one would not.)
Last edited on
I tried moving lines of code around and found that the red x follows the loopct++ line wherever I move it within the while loop, if that's any help.
(Before anyone tells me, yes I know this post is a duplicate of another post, also by me, in a different forum. I figured two hooks in the water might catch a fish where one would not.)


Well please don't do that, it's a waste of time for respondents. If no one has replied you can delete the other post, otherwise direct people to whichever one you are going to go with.

Ideally post a minimal amount of compile-able code that demonstrates the problem. Use code tags, so we can compile with cpp.sh, and see line numbers. You could instead just post all of your code.

http://www.cplusplus.com/articles/z13hAqkS/

The New Post page is broken, but you can edit the post and put the code tags in, or type them in at the start.

The debugger is not pointing to anything in the code, but rather popping up an x in white space and giving me this message:


Just so you know, a compiler is not a debugger :+) You probably have a GUI debugger in your IDE. One has a watch list of variables, step through the code 1 line at a time, see the values change, deduce what went wrong.
Thanks. I did delete the other post. I could easily just post the whole code in here. I just figured I would get a "TLDR".

For reference, I am using Visual Studio 2017, which has a built in GUI Debugger with literally hundreds of flags, and I have no idea what any of them mean at all.

I have been trying for hours to debug using common sense, my textbook, the c++ for dummies guide I bought, emailing friends, classmates, even my teacher (who didn't respond and won't until after the assignment is late), and nobody so far has been able to point out my problem. That being the case, here is the entire assignment's code, with referencing line numbers, omitting the beginning comments where I have my name and assignment number and stuff like that, in the hopes that somebody on here can:


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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
  
double mean(int v[], int size);
// PRE: size of array > 0
// POST: return average of values in array

double stddev(int v[], int size, double m);
// PRE: size of array > 0, 0 <= m <= 100
// POST: return standard deviation of values in array with mean m

char letter(int score, double m, double s);
// PRE: 0 <= score, m, s <= 100
// POST: return letter grade for score with mean m and standard deviation s

string code(string id, string lastname, string firstname);
// PRE: id is seven digit identification
// POST: return code of first letter of last name, first letter of first name, last two digits of id


int main()
{
	ifstream fin;
	ofstream fout;
	fin.open("grades.txt");
	fout.open("classgrades.txt");
	const int CAPACITY = 24;			// capacity of the array
	string codes[CAPACITY];           	// array of id codes
	int midterms[CAPACITY];				// array of midterm grades
	int finals[CAPACITY];				// array of final grades
	string id;							// 7-digit id code
	string lastname;
	string firstname;
	string major;
	string year;						// Student's year of school
	int midterm;
	int final;
	string idcode;
	double finalmean;					// Average grade on the final
	double finalstddev;					// Standard deviation among final grades
	double midtermmean;					// Average grade on the midterm
	double midtermstddev;				// Standarn deviation among midterm grades
	char finalgrade[CAPACITY];			// An array of the letter grades for the final
	char midtermgrade[CAPACITY];		// An array of the letter grades for the midterm
	int ct = 0;
	while (!fin.fail(),ct<=24)
	{
		ct++;
		fin >> id;
		fin >> lastname;
		fin >> firstname;
		fin >> major;
		fin >> year;
		fin >> midterm;
		fin >> final;
		midterms[ct - 1] = midterm;
		finals[ct - 1] = final;
		idcode = code(id, lastname, firstname);
		codes[ct - 1] = idcode;
	}
	finalmean = mean(finals, CAPACITY);
	midtermmean = mean(midterms, CAPACITY);
	finalstddev = stddev(finals, CAPACITY, finalmean);
	midtermstddev = stddev(midterms, CAPACITY, midtermmean);
	for (int i = 1; i <= CAPACITY; i++)
	{
		finalgrade[i - 1] = letter(finals[i - 1], finalmean, finalstddev);
		midtermgrade[i - 1] = letter(midterms[i - 1], midtermmean, midtermstddev);
	}
	fout << "Grade Calculations" << endl << "By Shane Blodgett" << endl;
	for (int i = 1; i <= CAPACITY; i++)
	{
		fout << setprecision(1);
		fout << setw(15) << codes[i - 1] << setw(15) << midterms[i - 1]
			<< midtermgrade[i - 1] << setw(15) << finals[i - 1]
			<< finalgrade[i - 1] << endl;
	}
	fout << endl << endl;
	fout << "The class mean for the midterm exam was a " << midtermmean << endl;
	fout << "The standard deviation amongst the midterm exams was +/- " << midtermstddev << endl;
	fout << endl << endl;
	fout << "The class mean for the final exam was a " << finalmean << endl;
	fout << "The standard deviation amongst the final exams was +/- " << finalstddev << endl;
	fin.close();
	fout.close();
 	return 0;
}
 
string code(string id, string lastname, string firstname)
// PRE: id is seven digit identification
// POST: return code of first letter of last name, first letter of first name, last two digits of id
{
	char firstletterfirst = firstname[0];
 	char firstletterlast = lastname[0];
 	char sixthdigitid = id[5];
 	char seventhdigitid = id[6];
 	string idcode = { firstletterfirst, firstletterlast, sixthdigitid, seventhdigitid };
 	return idcode;
}
 
double mean(int v[], int size)
// PRE: size of array > 0
// POST: return average of values in array
{
 	 int sum = 0;
 	 for (int i = 1; i <= size; i++)
    {
 		sum = sum + v[i - 1];
    }
 	 double mean = sum / size;
 	 return mean;
}
 
double stddev(int v[], int size, double m)
{
          double sumofsquares = 0;
 	  double spread[24];
 	  double var;
 	  double stddev;
 	  for (int i = 1; i < size; i++)
	{
 		spread[i - 1] = (v[i - 1] - m)*(v[i - 1] - m);
 		sumofsquares = sumofsquares + spread[i - 1];
 	}
 	  var = sumofsquares / 23;
 	  stddev = sqrt(var);
 	  return stddev;
}
 
char letter(int score, double m, double s)
// PRE: 0 <= score, m, s <= 100
// POST: return letter grade for score with mean m and standard deviation s
{
 	  char grade;
 	  if (score < (m - (2 * s)))
 		grade = 'F';
 	  else if (score >= (m - (2 * s)) && score < (m - s))
 		grade = 'D';
 	  else if (score >= (m - s) && score < m)
 		grade = 'C';
 	  else if (score >= m && score < (m + 0.67*s))
 		grade = 'B';
 	  else
 		grade = 'A';
 	  return grade;
}
Last edited on
for (int i = 1; i <= CAPACITY; i++)

This code goes out of bounds for the array. In this case the bounds are 0 to 23, so when it gets to 24 it's out of bounds. Simply change it to this:

for (int i = 0; i < CAPACITY; i++)

That is the idiom of a for loop.

In future, can you learn to use code tags, as I mentioned earlier, the code should look like this:


1
2
3
4
5
6
7
8
9
10
 #include <iomanip>
 #include <iostream>
 #include <fstream>
 #include <string>

 using namespace std;

 double mean(int v[], int size);
 // PRE: size of array > 0
// POST: return average of values in array 
Last edited on
This loop condition while (!fin.fail(),ct<=24) is not right.

We want to keep looping as long as
a. the capacity of the array has not been reached
b. a set of values have been successfully read

1
2
3
4
5
6
7
8
9
10
11
    int ct = 0;

    while( ct < CAPACITY && fin >> id >> lastname >> firstname >> major >> year >> midterm >> final )
    {
        midterms[ct] = midterm;
        finals[ct] = final;
        idcode = code(id, lastname, firstname);
        codes[ct] = idcode;

        ++ct ;
    }


You may want to perform a sanity check (that size of id is at least seven),
before trying to compute the code (by accessing id[5] and id[6]).
Last edited on
Two things I noticed with at a quick glance:

while (!fin.fail(),ct<=24)

One that comma is probably not doing what you think it is, you need to use a logical operator not a comma.

Two you would have a possible buffer overflow error (if that comma was doing what you think it is) because of the use of the <= operator. Remember arrays start at zero and end at size - 1, not size. You seem to be making this same mistake several times in your code.

Also when you get your crash you should be able to start the debugger by pressing the correct key, the debugger should be able to tell you exactly where it detected the problem and allow you to view the state of the variables at the time of the crash.

Lastly please post your code without the line numbers inside code tags (press the <> icon to the right of the edit window and insert your code between the tags.


Wow, thank you guys so much. I can't help but feel completely blind. This issue was fixed by simply changing lines 60 and 62 to the following:

while (!fin.fail(),ct<CAPACITY)

++ct;

The code ran fine and finally gave me the output I needed. Thank you so much!
I also retroactively tagged the code. So sorry, I didn't see the tags the first time, and I was just copying and pasting the second time. My bad.
Topic archived. No new replies allowed.