Program has stopped working

My program keeps crashing. Every time I run the program, it'll print the original sales array, process the sales transaction and add the totals into the sales array, then crash before printing the updated sales array =S


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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
int main()
	{
	SalesRegion region("Illinois", "sellersIL.txt");

	region.print();

	region.processSales("salesILJuly.txt");

	region.print();

	return 0;
	}	 
		
//********************************************************************************
Seller::Seller() //default constructor for the Seller Class
	{
	setFirstName("None");
	setLastName("None");
	setID("ZZZ000");
	setSalesTotal(0);
	}

//********************************************************************************
Seller::Seller(string newFirstName, string newLastName, string newID, double newSalesTotal) //contructor for seller class
	{
	setFirstName(newFirstName);
	setLastName(newLastName);
	setID (newID);
	setSalesTotal(newSalesTotal);
	}

//********************************************************************************
void Seller::setFirstName(string newFirstName) //check if the length of newFirstName is greater than 0
	{
	if (newFirstName.length() > 0)
		{
		firstName = newFirstName;
		}
		
	else
		firstName = "None";
	}

//********************************************************************************
void Seller::setLastName(string newLastName) //check if the length of newLastName is greater than 0
	{
	if (newLastName.length() > 0)
		{
		lastName = newLastName;
		}
		
	else
		lastName = "None";
	}

//********************************************************************************
void Seller::setID(string newID) //check if the length of newID is greater than 0
	{
	if (newID.length() > 0 && newID.length() < 7)
		{
		sellerID = newID;
		}
		
	else
		sellerID = "ZZZ000";
	}

//********************************************************************************
void Seller::setSalesTotal(double newSalesTotal) //checks if the sales is greater than 0
	{
	if (newSalesTotal> 0)
		{
		salesTotal = newSalesTotal;
		}
		
	else
		salesTotal = 0;
	}

//********************************************************************************
string Seller::getFirstName() //returns the seller's first name
	{
	return firstName;
	}

//********************************************************************************
string Seller::getLastName() //returns the seller's last name
	{
	return lastName;
	}

//********************************************************************************
string Seller::getID() //returns the seller's id
	{
	return sellerID;
	}

//********************************************************************************
double Seller::getSalesTotal() //returns the seller's sales total
	{
	return salesTotal;
	}

//********************************************************************************
void Seller::print()  //prints data members of seller into formatted columns
	{
	cout << fixed << setprecision(2);
	
	cout << left << setw (25) << lastName + ", " + firstName << " " <<  setw(9) << sellerID
		 << right << setw (10) <<salesTotal << endl;
	}

//********************************************************************************
void SalesRegion::sortSellersByID() //sorts selers in ascending order by seller id
	{
	int i;
	int j; 
	int min;
	Seller temp;
   
	for (i = 0; i < numSellers - 1; i++)
    	{
    	min = i;
      
    	for (j = i+1; j < numSellers; j++)
        	{
        	if (sellerArray[j].getID() < sellerArray[min].getID())
            min = j;
        	}
         
		temp = sellerArray[i];
		sellerArray[i] = sellerArray[min];
		sellerArray[min] = temp;
		}
	}

//********************************************************************************
int SalesRegion::findSellerID(string searchID) // finds the position of the seller array element with a seller ID that matches search id
	{
	int i;

	for (i = 0; i < numSellers; i++)
		{
		if (searchID == sellerArray[i].getID())
		return i;
		}

	return -1;
	}

//********************************************************************************
SalesRegion::SalesRegion() //default constructor for the Sales Region Class
	{
	numSellers = 0;
	}

//********************************************************************************
SalesRegion::SalesRegion(string newRegionName, string fileName) //constructor for the Sales Region Class, builds sales array
	{
	ifstream inFile;
	string firstName;
	string lastName;
	string sellerID;
	double salesTotal;
	
	numSellers = 0;
	setRegionName (newRegionName);
	
	inFile.open(fileName.c_str());
	if (!inFile)
		{
		cout << "Error - unable to open input file " << fileName << endl;
		exit(1);
		}
		
	inFile >> lastName;
	while (inFile)
		{
		inFile >> firstName;
		inFile >> sellerID;
		inFile >> salesTotal;
		
		Seller newSeller (firstName, lastName, sellerID, salesTotal);
		
		sellerArray [numSellers] = newSeller;
		numSellers++;
		
		inFile >> lastName;
		}
	
	inFile.close();
	
	sortSellersByID ();
	}
//********************************************************************************
void SalesRegion::setRegionName(string newRegionName) //checks if the lenght of the newRegionName is greater than 0
	{
	if (newRegionName.length() > 0)
		{
		regionName = newRegionName;
		}
		
	else
		regionName = "None";
	}


//********************************************************************************
string SalesRegion::getRegionName() //returns the region name
	{
	return regionName;
	}


//********************************************************************************
void SalesRegion::processSales(string fileName) //reads a series of sales records and processes them into the seller array
	{
	ifstream inFile;
	string id; //seller's id
	double salesAmount; //sales of the transaction made

	
	cout << endl
		 << internal << setw(22) << "Sales Transactions" << endl
		 << endl
		 << left << setw(15) << "Seller ID" << "Sales Amount" <<endl
		 << endl;

	inFile.open(fileName.c_str());
	if (!inFile)
		{
		cout << "Error - unable to open input file\n";
		exit(1);
		}
	
	inFile >> id;
	while (inFile)
		{
		inFile >> salesAmount;
		
		int index = findSellerID (id); //proccesses the sales transaction to the proper element in the sellerArray
	
		if (index == - 1)
			{
			cout << left << setw (15) << id << right << setw(10) << "Error - ID not found" <<endl;
			}
							
		sellerArray[index].setSalesTotal(sellerArray[index].getSalesTotal() + salesAmount);
			 
		cout << left << setw (15) << id << right << setw(10) << salesAmount << endl;
		
		inFile >> id;
		}	 
		
	inFile.close ();
	
	cout << endl;
	}


//********************************************************************************
void SalesRegion::print() //prints the seller array
	{
	cout << internal << setw(30)<< "Sales Listing for " << regionName << endl <<endl;
	
	int i;
  
	for (i = 0; i < numSellers; i++)
		{
		sellerArray[i].print();
		}

	}

When you say crash...do you mean the window closes before you can see what it prints? Or are you actually getting an run-time error?
http://tinypic.com/r/2ji2l1/6

If you can see at the link, print the sales array one time, then print the sales transaction, then stops working as it begins to print the updated sales array.
Since your program is hanging at this spot each time, I'd take a look at what's causing this:
cout << internal << setw(30)<< "Sales Listing for " << regionName << endl <<endl;

I'm not sure why that would be giving you errors, everything looks kosher. Try commenting that line out and see if the program continues.

Edit: I believe you may have an issue with being out of bounds here, not 100% sure:
1
2
3
4
5
6
7
8
9
10
		if (index == - 1)
			{
			cout << left << setw (15) << id << right << setw(10) << "Error - ID not found" <<endl;
			}
							
		sellerArray[index].setSalesTotal(sellerArray[index].getSalesTotal() + salesAmount);
			 
		cout << left << setw (15) << id << right << setw(10) << salesAmount << endl;
		
		inFile >> id;


If the index equals -1, you're still looking at the sellerArray[-1]. I believe that you want to have and else added:
1
2
3
4
5
6
7
8
9
10
11
		if (index == - 1)
			{
			cout << left << setw (15) << id << right << setw(10) << "Error - ID not found" <<endl;
			}
		else
			{ // Added here					
			sellerArray[index].setSalesTotal(sellerArray[index].getSalesTotal() + salesAmount);
			 
			cout << left << setw (15) << id << right << setw(10) << salesAmount << endl;
			} // End here
		inFile >> id;
Last edited on
The still refuses to continue even with that line omitted. I tried omitting the regionName in that line however the program still stops and doesn't even print the "Sales Listing for " part.
Read my edit.
I just did, and the program worked great. Thank you very much. Not sure why it happened this time. The this assignment was converting the last program I wrote from structures into object-oriented program.
Maybe you didn't have any error indexs in the last one. Or simply you forgot to add the else here. But I'm glad you got it resolved. You can now mark the topic solved.

Happy Programming. =)
Topic archived. No new replies allowed.