file not being read in

I made some of the recommended edits and it still isn't changing those errors. I think it has something to do with my readem function i dont think it's reading in the data. if I loop through each element within the array it reads their declared values and prints them. I dont think it's reading anything in from my input file.

throw an error and it breaks at readem and i get the message "Unhandled exception at 0x796268D3 (msvcp140d.dll) in testproject1.exe: 0xC0000005: Access violation writing location 0x00940004."

pretty simple program to start out my cs2 class. However it's telling me i have not initialized 3 of the variables in my struct but I dont know what to change to fix it, or what to even look up. any help would be much appreciated, Thanks in advance.
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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
const int maxc = 50;
struct Ctype
{
	int crn;
	string course;
	int credithours, numstu;
	string proff;
};
const Ctype initrec = { 0, "course", 0, 0, "proff" };
void initem(Ctype c[], int& numc)
{
	int i;
	for (i = 0; i < maxc; i++) c[i] = initrec;
	numc = 0;

}

void readem(Ctype c[], int& numc)
{
	int i;
	ifstream inf("project1.dat");
	i = 0;
	while (!inf.eof())
	{
		inf >> c[i].crn >> c[i].course >> c[i].credithours >> c[i].numstu >>
			c[i].proff;
		i++;
	}
	numc = i;

}
void printem(Ctype c[], int numc, ofstream & outf)
{
	int i;
	for (i = 0; i < numc; i++)
	{
		outf << setw(34) << "MATH DEPARTMENT" << endl << setw(30) << "Credit" << setw(12) << "Number of"
			<< endl << left << setw(8) << "CRN" << right << setw(10) << "Course" << setw(11) << "Hours"
			<< setw(13) << "Students" << setw(13) << "Professor" << endl << left << setw(8) << c[i].crn
			<< setw(15) << c[i].numstu << right << setw(6) << c[i].credithours << setw(13) << c[i].numstu
			<< setw(3) << " " << left << c[i].proff << endl;
	}
	outf << endl << endl;
}

void swapem(Ctype & a, Ctype & b)
{
	Ctype temp;
	temp = a;
	a = b;
	b = temp;
	cout << "swapem created" << endl;

}

void sortem(Ctype c[], int numc)
{
	int i, j;
	for (j = 0; j < numc - 1; j++)
		for (i = 0; i < numc - 1; i++)
			if (c[i].credithours > c[i + 1].credithours) swapem(c[i], c[i + 1]);
	cout << "swapem created" << endl;

}

void getAvg(Ctype c[], int numc, ofstream & outf)
{
	int i;
	double average;
	average = 0;
	for (i = 0; i < numc; i++) average += c[i].numstu;
	average /= numc;
	outf << " The average number of students per class is " << average
		<< endl << endl;


}

int main()
{
	Ctype c[maxc];
	int numc;
	ofstream outf("project1.ot");
	initem(c, numc);
	readem(c, numc);
	printem(c, numc, outf);
	sortem(c, numc);
	getAvg(c, numc, outf);
	printem(c, numc, outf);
	return 0;
	system("pause");
}
Last edited on
Either:
8
9
10
11
12
13
14
15
struct Ctype
{
   int crn         = 0;
   std::string course;
   int credithours = 0;
   int numstu      = 0;
   std::string proff;
};


Or:
8
9
10
11
12
13
14
15
struct Ctype
{
   int crn         { };
   std::string course;
   int credithours { };
   int numstu      { };
   std::string proff;
};

Now your int data members are properly initialized.
Make crn, credithours, and numstu initialized with zero in the struct.

Also you should avoid using fixed sized arrays for things that are not fixed sized.
Topic archived. No new replies allowed.