arrays do not display expected results

I think this is the easiest way to explain my problem here is the code I have built
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
#include <iostream>
#include <fstream>

using namespace std;

int gtut (int, int);//protoype grad
int utut (int, int);//prototype undergrad tuition

int main()
{

ifstream input;
ofstream results;
input.open("/Users/putyoursoxon/Documents/input.txt");//input file
results.open("/Users/putyoursoxon/Documents/results.txt");//application results go here
int loopnum, i, idnum, state, credits, gd, datasum, dataavg, j;
char study;
gd = 1;
int idnuma[gd];
int statea[gd];
int creditsa[gd];
int rega[gd];
char studya[gd];

	input >> loopnum;//number of students to be evaled

	datasum = 0;
	
	for (i = 0; i < loopnum; i ++)
{
input >> idnum >> state >> study >> credits;

	if (study != 'u' && study != 'g' || state != 0 && state != 1 || credits <= 0 || idnum < 1 || idnum > 999)//error checking
	{
		results << "There is an error in the input data for student " << idnum << endl;
		if (study != 'u' && study != 'g')
		{
			results << "The character " << study << " is not a valid input for the Status of Study. Please input a u for undergrad or a g for graduate student" << endl;
			results << endl;
		}
		if (state != 0 && state != 1)
		{
			results << "The Character " << state << " is not a valid input for the Status of Residence.  Please input a 0 for in-state or a 1 for out-of-state" << endl;
			results << endl;
		}
		if (credits <= 0)
		{
			results << credits << " is not a valid credit load" << endl;
			results << endl;
		}
		if (idnum < 0 || idnum > 999)
		{
		results << idnum << " is not a valid Student id number" << endl;
			results << endl;
		}
	}
	else
	{
	
	if (studya[gd-1] == 'u')
		rega[gd-1] = utut(creditsa[gd-1], statea[gd-1]);//undergrad tuition called
	if (studya[gd-1] == 'g')	
		rega[gd-1] = gtut(creditsa[gd-1], statea[gd-1]);//grad tuition called
		datasum = datasum + rega[gd-1];
		
		gd ++;
	
		}
}
	
	results << "Student ID#        Residence         Study          Credit#        Fee" << endl;
	results <<"------------        ---------         -----          -------        ---" << endl;
	for (j = 0; j< gd; j++)
	{
	results << idnuma[j] << "                ";
		if (statea[j] == 1)
			results << "Out-of-state      ";
		if (statea[j] == 0)
			results << "In-state          ";
		if (studya[j] == 'u')
			results << "Undergraduate  ";
		if (studya[j] == 'g')
			results << "Graduate       ";
		results << creditsa[j] <<"               ";
		results << rega[j] << endl;
		
	}
	dataavg = datasum / gd;
	results <<"The average registration fee is:  $" << dataavg << endl;
	


	input.close();
	results.close();
	return 0;
}
int gtut (int fcred, int fstate)//grad tuition
{
	int freg;
	freg = 0;
	if (fcred <= 9)
		freg = freg + (fcred * 400);
	if (fcred > 9)
		freg = freg + 3600;//grad pays max 9 creds for tuiton
	if (fstate == 0)
		freg = (freg * 0.35);
	if (fcred >= 12)//general fees
		freg = freg + 516;
	if (fcred < 12)
		freg = freg + (fcred * 43);
	freg = freg + 10;//activities fee
	return freg;
}

int utut (int fcred, int fstate)//undergrad tuition
{
	int freg;
	freg = 0;
	freg = freg + (fcred * 280);
	if (fstate == 0)
		freg = (freg * 0.35);
	if (fcred >= 12)//general fees
		freg = freg + 516;
	if (fcred < 12)
		freg = freg + (fcred * 43);
	return freg;
}
	


and here is the input file
12
1234 1 u 13
234 2 g 12
119 0 f 20
560 0 g -17
450 0 g 20
111 0 g 6
999 1 g 14
767 1 g 6
345 0 u 20
498 0 u 8
970 1 u 18
383 1 u 8


and the results file
There is an error in the input data for student 1234
1234 is not a valid Student id number

There is an error in the input data for student 234
The Character 2 is not a valid input for the Status of Residence. Please input a 0 for in-state or a 1 for out-of-state

There is an error in the input data for student 119
The character f is not a valid input for the Status of Study. Please input a u for undergrad or a g for graduate student

There is an error in the input data for student 560
-17 is not a valid credit load

Student ID# Residence Study Credit# Fee
------------ --------- ----- ------- ---
16493 0 0
9090 In-state 0 16493
-1073743800 0 -1073745352
9207 0 -1073745116
-1073744676 0 0
0 0 0
48 0 0
0 0 0
0 -1073745200 0
The average registration fee is: $-238608219

it is detecting errors fine, I just dont understand what is happening in the array, can anybody point me in the right direction?
Thanks

I think you forgot to give some value to creditsa[gd-1] and statea[gd-1].
i added this under the else statement

1
2
3
4
5
6
7
8
9
10
11
12
13
else
	{
		idnum = idnuma[gd-1];
		state = statea[gd-1];
		study = studya[gd-1];
	if (studya[gd-1] == 'u')
		rega[gd-1] = utut(creditsa[gd-1], statea[gd-1]);//undergrad tuition called
	if (studya[gd-1] == 'g')	
		rega[gd-1] = gtut(creditsa[gd-1], statea[gd-1]);//grad tuition called
		datasum = datasum + rega[gd-1];
		
		gd ++;
	

but Im still getting these jumbled results. Cant figure out what is wrong



There is an error in the input data for student 1234
1234 is not a valid Student id number

There is an error in the input data for student 234
The Character 2 is not a valid input for the Status of Residence. Please input a 0 for in-state or a 1 for out-of-state

There is an error in the input data for student 119
The character f is not a valid input for the Status of Study. Please input a u for undergrad or a g for graduate student

There is an error in the input data for student 560
-17 is not a valid credit load

Student ID# Residence Study Credit# Fee
------------ --------- ----- ------- ---
16493 0 0
9050 In-state 0 16493
-1073743800 0 -1073745352
9167 0 -1073745116
-1073744676 0 0
0 0 0
48 0 0
0 0 0
0 -1073745200 0
The average registration fee is: $-238608219


in lines 18 - 23 you initialize gd, and your arrays, however, i could not find anywhere in your code where you used dynamic memory allocation to increase your array size. thus:
1
2
3
4
5
6
gd = 1;
int idnuma[gd];
int statea[gd];
int creditsa[gd];
int rega[gd];
char studya[gd];

initializes each array to only have a size of one... meaning it's not really an array, it's just one value. so on line 66, when you increment gd, you are actually effectively going past the end of the array. anything past the end of the array is not guaranteed to be valid, and overwriting data past the end of an array can cause the program to crash. So those weird values you are getting are most likely due to accessing memory blocks past the end of the array (which c will allow you to do) that do not contain any relevant data. Hopefully this helped!
Yeah, if you want something that can be dynamically changed, I would suggest using std::vector for it.
ok, so i changed this
1
2
3
4
5
6
input >> loopnum;//number of students to be evaled
	int idnuma[loopnum];
	int statea[loopnum];
	int creditsa[loopnum];
	int rega[loopnum];
	char studya[loopnum];


and put this later on this gd int to 0
1
2
3
4
5
6
7
8
9
10
11
12
else
	{
		idnum = idnuma[gd];
		state = statea[gd];
		study = studya[gd];
	if (studya[gd] == 'u')
		rega[gd] = utut(creditsa[gd], statea[gd]);//undergrad tuition called
	if (studya[gd] == 'g')	
		rega[gd] = gtut(creditsa[gd], statea[gd]);//grad tuition called
		datasum = datasum + rega[gd];
		
		gd ++;

but my results file is still f'ed up, now I'm lost, can anybody help?
if I want to use a vector, how exactly word I set it up, is it like a array, I havent learned about vectors yet, I tried to use the reference, but im a little confused
ok i figured it out, idiot mistake, i was doing
idnum = idnuma[gd]
instead of the correct
idnuma[gd] = idnum
etc etc

another question I have though is if I want to sort thru the list I now have a array that is (loopnum) long but only has data for the amount of (gd), so there is empty space in the array, can I still search it? Like if I wanted to list people who had under twelve credits, wouldnt the empty array spaces mess up the results??
If you have data in those spots, it could screw it up. That's why a vector would be a better idea...anyway, you can look here for info on how to use vectors:

http://www.cplusplus.com/reference/stl/vector/
Topic archived. No new replies allowed.