Stack Problems

My num array is corrupted, can't figure out while.

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void openfiles(ifstream& infile,ofstream& outfile);
void file_input(ifstream& infile, int num[], double value[], char&type);
void calculations(int num[], double value[], char type);
void output(ofstream& outfile,int num[], double value[]);

int main()
{
	int num[4];
	double value[5];
	char type;

	ifstream infile;
	ofstream outfile;
	openfiles(infile, outfile);
	file_input(infile, num, value, type);
	while(infile)
	{
		calculations(num, value, type);
		file_input(infile, num, value, type);
	}
	output(outfile, num, value);
	return 0;
}

//PreConditions: The input file has been created
//PostConditions: INLABVI.DAT and OUTLABVI.OUT have been opened

void openfiles(ifstream& infile,ofstream& outfile)
{
	infile.open("INLABVIII.DAT");
	outfile.open("OUTLABVIII.OUT");
	if (!infile || !outfile)
	{
		cout<<"Caution error opening files" << endl;
	}
}

//PreConditions: INLABVI.DAT has been created
//PostConditions: CDt and CDv have been inputed via INLABVI.DAT

void file_input(ifstream& infile, int num[], double value[], char&type)
{
	infile>>type>>value[5];
}

//PreConditions: CDt and CDv have been inputed via INLABVI.DAT
//PostConditions: The value of CD has increased by 1, CD type and Value has been added to their respective collection, and finally the total value of the whole
// collection has been calculated

void calculations(int num[], double value[], char type)
{
	num[5]++;
	switch(type)
	{
	case 'J':
		{
			num[0]++;
			value[0]=value[0]+value[5];
			break;
		}
	case 'R':
		{
			num[1]++;
			value[1]=value[1]+value[5];
			break;
		}
	case 'C':
		{
			num[2]++;
			value[2]=value[2]+value[5];
			break;
		}
	case 'N':
		{
			num[3]++;
			value[3]=value[3]+value[5];
			break;
		}
	default:
		break;
	}
	value[4]=value[0]+value[1]+value[2]+value[3];
}

//PreConditions: OUTLABVI.OUT has been opened. The total value, total # of CDs, the total # of Jazz CDs and that value, the total # of Rock and Roll CDs
//and that value, the total # of Classical CDs and that value, and the total # of Country CDs and that value have been calculated.
//PostConditions: The types of CDs, the total # of those types, the total value of those types, and a running total of all CDs and their value have been output
//to the file OUTLABVI.OUT in the appropriate format.

void output(ofstream& outfile, int num[], double value[])
{
	int v,w,x,y,z;
//////////////////////////////////// v width below
	if(value[0] <=9)
	{
		v=11;
	}
	else if (value[0] <=99)
		{
			v=10;
		}
		else if (value[0] <=999)
		{
			v=9;
		}
			else
			{
				v=8;
			}
//////////////////////////////////// w width below
	if(value[1] <=9)
	{
		w=11;
	}
	else if (value[1] <=99)
		{
			w=10;
		}
		else if (value[1] <=999)
		{
			w=9;
		}
			else
			{
				w=8;
			}
///////////////////////////////////// x width below
	if(value[2] <=9)
	{
		x=11;
	}
	else if (value[2] <=99)
		{
			x=10;
		}
		else if (value[2] <=999)
		{
			x=9;
		}
			else
			{
				x=8;
			}
//////////////////////////////////// y width below
	if(value[3] <=9)
	{
		y=11;
	}
	else if (value[3] <=99)
		{
			y=10;
		}
		else if (value[3] <=999)
		{
			y=9;
		}
			else
			{
				y=8;
			}
//////////////////////////////////// z width below
	if(value[4] <=9)
	{
		z=11;
	}
	else if (value[4] <=99)
		{
			z=10;
		}
		else if (value[4] <=999)
		{
			z=9;
		}
			else
			{
				z=8;
			}
//////////////////////////////////////
	outfile<<setprecision(2)<<showpoint<<fixed;
	outfile<<"Type of CD         Number        Value"<<endl;
	outfile<<"  Jazz"<<setw(17)<<num[0]<<setw(v)<<"$"<<value[0]<<endl;
	outfile<<"  Rock"<<setw(17)<<num[1]<<setw(w)<<"$"<<value[1]<<endl;
	outfile<<"  Classical"<<setw(12)<<num[2]<<setw(x)<<"$"<<value[2]<<endl;
	outfile<<"  Country & Western"<<setw(4)<<num[3]<<setw(y)<<"$"<<value[3]<<endl;
	outfile<<"  Totals"<<setw(15)<<num[4]<<setw(z)<<"$"<<value[4]<<endl;
	system( "notepad.exe OUTLABVI.OUT" );
}
Last edited on
You are going out of bounds on both of your arrays, arrays start at 0 and go to 1 less than the size defined. When you declare int num[4] you access the elements by 0,1,2,3.

You are trying to do stuff like num[5]++ which will cause memory errors. Same for 'value' array in several places. It also looks like you are trying to increment the values of num without assigning any values to it before hand.
Yeah not sure why I threw a 5 in there, thanks for having a better eye out for it then me!
Topic archived. No new replies allowed.