structure not working

I am trying to incorporate the following information into a structure:
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
45256	Rodrigues	Joana		58	75	58	61	59	75	63	92
37915	Wright		Michelle	98	83	56	62	63	90	57	67
81984	Williams	Jenny		55	67	54	63	89	84	93	75
73984	Phaneuf		Lesley		78	85	57	51	68	94	51	83
80886	Laflamme	Nicole		76	51	71	94	69	78	87	91
39473	Kenyon		Patricia	65	54	90	68	94	70	95	97
12127	McCabe		Kelly		51	96	0	64	54	75	71	94
52458	Whitten		Sarah		99	58	94	82	81	75	82	70
77921	Connors		Sarah		52	58	88	63	61	65	78	78
28810	Navin		Joshua		94	75	62	93	64	92	87	94
31571	Toporowski	Crystal		93	0	77	77	63	68	88	58
33580	Ziolko		John		74	64	98	92	98	89	0	79
14508	Stronach	Kurt		80	95	96	84	78	86	53	59
44520	Ecklord		Ryan		0	61	56	86	98	98	59	83
15246	Berling		Danielle	85	64	0	75	69	0	54	85
16137	Littlefield	Arionna		71	85	74	97	69	64	82	95
62631	Niedojadlo	Evan		73	66	83	97	97	51	66	88
59640	Knieriem	Brandon		52	87	97	66	97	90	93	56
44102	Spence		Arthur		85	84	100	82	91	0	95	62
42331	Rose		Nicole		83	50	96	68	54	62	0	93
72054	Houde		Jessica		72	66	71	62	75	71	86	100
24609	Cooper		Camille		62	93	92	72	58	76	57	66
85736	Hepburn		Spencer		71	0	62	60	52	62	64	51
87129	Morang		Nicholas	77	67	57	89	88	64	68	78
43865	Hildebrandt	Stephenson	97	94	93	93	82	88	78	100
78575	Suslovic	Vikilynn	65	54	74	67	61	76	69	76
58751	Flores		Jose		58	80	0	74	87	95	96	69
18140	Galotti		Salvator	78	65	90	66	88	57	93	98
10364	Denaro		Tony		73	85	55	72	68	91	55	89
99189	Vasquez		Oskar		66	60	0	75	63	92	92	52
21705	Roy		Jake		86	0	89	94	82	0	60	98
50838	Burr		Jermiah		83	84	94	90	77	73	72	63
32998	Benway		Eileen		76	98	80	69	75	94	77	95
87361	Perlmutter	Diadre		96	60	81	88	81	53	91	74


with the following code:

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

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

struct student
{
	int id;
	string fname;
	string lname;
	int gr[6];
	double avg;
	char letGr;
};

void print(student arr[], int c)
{
	cout << endl;
	for (int i=1;1<=c;i++)
	{
		cout << setw(2) << i << ' ' << left << setw(10) << arr[i].id
			<< arr[i].fname << arr[i].lname << right;
		for (int j=1;j<=5;j++)
			cout << setw(5) << arr[i].gr[j];
		cout << setw(6) << arr[i].avg << setw(3) << arr[i].letGr << endl;
	}
	cout << endl;
}


void main()
{
	ifstream inF;
	student myStuds[20];
	int cnt=0;
	int id;
	string fname, lname;

	cout << fixed << setprecision(1);

	inF.open("students.txt");
	inF >> id;
	while (!inF.eof())
	{
		cnt++;
		myStuds[cnt].id = id;
		myStuds[cnt].fname = fname;
		myStuds[cnt].lname = lname;
		for (int i = 1; i < 5; i++)
			inF >> myStuds[cnt].gr[i];
		myStuds[cnt].avg = 0.0;
		myStuds[cnt].letGr = 'Y';
		inF >> id;
	}
	inF.close();
	cout << "Cnt = " << cnt << endl;
	print (myStuds, cnt);
}


My program compiles, but then it goes into the "test.exe has encountered a problem..." error.....any ideas what I need to fix?
student myStuds[20];


You allocate memory for 20 students, but there may be read much more (in your post there are 34 lines of data!).
Allocate more memory (sufficient for any possible amount of data) or use dynamically increasing container like std::vector<student> to store students.
I adjusted the number to 100, but it's still not working....I don't know how to do vectors, and I'm trying to stick with structures.....any other ideas?
Where are there operators reading fname and lname from file?

myStuds[cnt].fname = fname;
myStuds[cnt].lname = lname;

Here you assign empty string to the structure's fields and not read next portions from file, and then try to read them into integers gr[i].
I'm trying to read them straight from the file that is listed above.....even with = fname; and = lname; gone, the program still isn't working....
How fo you try to read them "straight"? Where is the corresponding inF >> operators?
Last edited on
Do I need to have those?
Yes, otherwise you are never reading any data from the file. As it is, it looks like you are just copying some uninitialized, probably garbage-valued variables.
well.....this kind of worked....it compiles without error, but all it does is bring me to a blank cmd.exe screen with a cursor....nothing else happens.....here is the code as it is updated:

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

struct student
{
	string id;
	string fname;
	string lname;
	int gr[5];
	double avg;
	char letGr;
};

void print(student arr[], int c)
{
	cout << endl;
	for (int i=1;1<=c;i++)
	{
		cout << setw(2) << i << ' ' << left << setw(10)<< arr[i].fname << arr[i].id 
			<< arr[i].lname << right;
		for (int j=1;j<=5;j++)
			cout << setw(5) << arr[i].gr[j];
		cout << setw(6) << arr[i].avg << setw(3) << arr[i].letGr << endl;
	}
	cout << endl;
}
double studAvg(student rec)
{
	int s=0;

	for (int i=1; i<=5; i++)
		s += rec.gr[i];
	return s / 5.;
}

double average(student arr[], int c, int i)
{
	double s=0;

	for (int j=1; j<=c; j++)
	{
		if (i > 0)
			s += arr[j].gr[i];
		else
			s += arr[j].avg;
	}
	return s/c;
}

char letterGrade(double sAvg, double cAvg)
{
	if (sAvg >= cAvg + 15) return 'A';
	else if (sAvg >= cAvg + 5) return 'B';
	else if (sAvg >= cAvg - 5) return 'C';
	else if (sAvg >= cAvg -15) return 'D';
	else return 'F';
}

void sort(student arr[], int c)
{
	student t;

	for (int i=c-1; i>=1; i--)
		for (int j=1; j <= i; j++)
			if (arr[j+1].fname < arr[j].fname)
			{
				t = arr[j]; arr[j] = arr[j+1]; arr[j+1] = t;
			}
}


void main()
{
	ifstream inF;
	student myStuds[50];
	int cnt=0;
	double classAvg;
	string fname, lname, id;

	cout << fixed << setprecision(1);

	inF.open("students.txt");

	inF >> id;
	while (!inF.eof())
	{
		cnt++;

		inF >> myStuds[cnt].id;
		inF >> myStuds[cnt].fname;
		inF >>myStuds[cnt].lname;
		for (int i = 1; i < 6; i++)
			inF >> myStuds[cnt].gr[i];
		inF >> myStuds[cnt].avg;
		inF >> myStuds[cnt].letGr;

		inF >> id;
	}
	inF.close();
	cout << "Cnt = " << cnt << endl;
	print(myStuds, cnt);
	sort(myStuds, cnt);
	classAvg = average(myStuds, cnt, 0);
	for (int i=1; i <= cnt; i++)
		myStuds[i].letGr = letterGrade(myStuds[i].avg, classAvg);
	print(myStuds, cnt);
	cout << endl;
}
You might have hit an infinite loop in your inF loop. Try just looping on !inF to get out of the loop if there were errors.
You read each id twice:
1
2
3
4
inF >> myStuds[cnt].id;
	...

		inF >> id;

I'm not too sure if it's an infinite loop or not....nothing is printing out at all....its just a stagnant black screen with a cursor and nothing else.....there isn't anything printed out, and the scroll bars aren't moving to show that it's a blank loop either....

I will try this suggestion, however, to see if it corrects the error....
ok....well, this is what I have now....it compiles without error, but now it goes straight into "Press any key to continue" instead of using my print function.....how do I make it so that I can see what has been read?

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

struct student
{
	int id;
	string fname;
	string lname;
	int gr[8];
	double avg;
	char letGr;
};

void print(student arr[], int cnt)
{
	cout << endl;
	for (int i=0;1<=cnt;i++)
	{
		cout << setw(2) << i << ' ' << left << setw(10)<< arr[i].fname << arr[i].id 
			<< arr[i].lname << right;
		for (int j=0;j<=5;j++)
			cout << setw(5) << arr[i].gr[j];
		cout << setw(6) << arr[i].avg << setw(3) << arr[i].letGr << endl;
	}
	cout << endl;
}
double studAvg(student rec)
{
	int s=0;

	for (int i=0; i<=8; i++)
		s += rec.gr[i];
	return s / 5.;
}

double average(student arr[], int cnt, int i)
{
	double s=0;

	for (int j=0; j<=cnt; j++)
	{
		if (i > 0)
			s += arr[j].gr[i];
		else
			s += arr[j].avg;
	}
	return s/cnt;
}

char letterGrade(double sAvg, double cAvg)
{
	if (sAvg >= cAvg + 15) return 'A';
	else if (sAvg >= cAvg + 5) return 'B';
	else if (sAvg >= cAvg - 5) return 'C';
	else if (sAvg >= cAvg -15) return 'D';
	else return 'F';
}

void sort(student arr[], int c)
{
	student t;

	for (int i=c-1; i>=1; i--)
		for (int j=1; j <= i; j++)
			if (arr[j+1].fname < arr[j].fname)
			{
				t = arr[j]; arr[j] = arr[j+1]; arr[j+1] = t;
			}
}


void main()
{
	ifstream inF;
	student myStuds[50];
	int cnt=0;
	string fname, lname, id;

	cout << fixed << setprecision(1);

	inF.open("students.txt");


	while (!inF.eof())
	{
		inF >> myStuds[cnt].id;
		inF >> myStuds[cnt].fname;
		inF >> myStuds[cnt].lname;
		for (int i = 0; i < 9; i++)
			inF >> myStuds[cnt].gr[i];
		inF >> myStuds[cnt].avg;
		int grade = 0;
		inF >> grade;
		myStuds[cnt].letGr = 'Y';


		cnt++;

	}
	inF.close();
	cout << "Cnt = " << cnt << endl;
	print (myStuds, cnt);
}
Last edited on
You have 8 gr in each line but read 9:
int gr[8]; for (int i = 0; i < 9; i++)

And where the column in the input file where grade is?

Adust input format with how you read data!
Topic archived. No new replies allowed.