[Solved] Error: *Exception thrown: write access violation. _Left was 0xCCCCCCCC.

[Solved]

When I try to run my C++ code, it does not work. It says:
Exception thrown: write access violation.
**_Left** was 0xCCCCCCCC.

Could you please tell me what I am doing wrong?
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
#include <iostream>
#include <string>
#include <iomanip> // setw(), setfill(), etc.
#include <fstream> // input/output file stream
using namespace std;

// Constants for the array lengths
const int studentArr = 2;
const int gradesLength = 4;


struct studentInfo {
	string fName;
	string lName;
	int g1;
	int g2;
	int g3;
	int g4;
};

int main() {
	int count = 0;
	studentInfo arr[6]; // fname, lname, g1, g2, g3, g4
	studentInfo grades[gradesLength]; // pos 0, 1, 2, 3 per student
	string inFile, outFile;
	cout << "Please enter an input file name: ";
	cin >> inFile;
	ifstream fin; // fin = fileInput
	fin.open(inFile); // inFile = holds input file name

	// Checks if file has opened successfully. If not it will do this:
	if (fin.fail()) {
		cout << "Couldn't find the given file." << endl;
		exit(0); // does this stop the program?
	}
	else { // If file has opened correctly, it will do this:
		// Program should read numbers of student records into an array.
		while (!fin.eof()) { // While it has not reached the end of the input file..
			// fin is like cin but for input files. Will allow us to output the text later.
			fin >> arr[count].fName >> arr[count].lName >>
				/* 
				The "arr" array stores all of the info (fName, lName, g1, g2, g3, g4)
				count will make sure all info from the read file is stored into the array.
				From my understanding, this array has multiple sections. Like so:
	
				Array Structure:
					fName
						- pos 0: John
						- pos 1: Amy
						- pos 2: Carol
					lName
						- pos 0: Doe
						- pos 1: Kargol
						- pos 2: Johnstone
					g1
						- pos 0: 90 (John Doe's first test grade)
						- pos 1: 80 (Amy Kargol's first test grade)
						- pos 2: 70 (Carol Johnstone's first test grade)
					g2
						- pos 0: 55 (John Doe's 2nd test grade)
						- pos 1: 82 (Amy Kargol's 2nd test grade)
						- Etc...
					Etc...
				*/
				arr[count].g1 >> arr[count].g2 >> arr[count].g3 >>
				arr[count].g4;
			count++; // acts as an i would in a for loop.
		}
		// Closing input file
		fin.close();

		// Opening output file
		cout << "Enter the output file name: ";
		cin >> outFile;
		ofstream fout;
		fout.open(outFile);
		
		if (!fout.fail()) { // if the output file has been opened successfully..?:
			// Writes to output file
			fout << setfill('=') << setw(78) << endl; 
			cout << 77 << endl;

			// Closing output file
			fout.close();
		}
	}
	// General Stuff
	cin.ignore(1000, '\n');
	cin.get();
	return 0;
}


Input File (csci112in.txt)
1
2
3
4
5
6
7
8
9
10
11
Johnny Lang 100 100 100 98
Suzie Wang 95 85 75 80
Allie Smith 85 85 80 82 
George Michael 0 100 100 0 
Henry Miller 75 85 82 90
Billie Jeans 100 92 83 85
Hillary Clinton 90 91 93 95
Sally Railey 65 70 75 78
Tom Jerry 85 90 97 98
Bill Kool 65 75 65 66
Alice Wood	44 75 88 99


Goal Output File (not there yet)
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
==============================================================================
fname          lname             test1   test2   test3   test4    avg.   grade
==============================================================================
Johnny         Lang                100     100     100      98   99.50       A
Tom            Jerry                85      90      97      98   92.50       A
Hillary        Clinton              90      91      93      95   92.25       A
Billie         Jeans               100      92      83      85   90.00       A
Bill           Kool                 75      85      95     105   90.00       A
Suzie          Wang                 95      85      75      80   83.75       B
Allie          Smith                85      85      80      82   83.00       B
Henry          Miller               75      85      82      90   83.00       B
Alice          Wood                 44      75      88      99   76.50       C
Sally          Railey               65      70      75      78   72.00       C
George         Michael               0     100     100       0   50.00       F
******************************************************************************
Class Average:                   73.09   86.18   85.27   79.18
Class Max:                         100     100     100      99
Class Min:                           0      70      65       0
==============================================================================
Total As:                   36%
Total Bs:                   27%
Total Cs:                   18%
Total Ds:                    9%
Total Fs:                    9%
==============================================================================

Thanks!
Last edited on
studentInfo arr[6];

You declared an array size 6. You have more data than that.

You made a comment, "acts as i would in a for loop." Why not use a for loop, though?

for (int count = 0; !fin.eof(); count++)
@Browni3141
Ohh okay. I was just thinking there are 6 types (fname, lname, g1, g2, g3, g4) and didn't realize their contents would count as well.

As for the for loop, I didn't realize I could use !fin.eof() in it. Thanks for pointing that out!

All fixed, thanks so much!
Topic archived. No new replies allowed.