array bounds checking while reading data

Hello everyone,
I have a program I am trying to tackle by breaking it down into workable parts, the program is complete but has an issue I can’t figure out. The overall program does the following: Program will determine the average and highest expenses, and the number of expenses higher than the average expenses, for all employees under each health care coverage plan and display the results.
The program consists of a text file and two arrays: emp[ ] and fam[ ], “O” for employees and “F” for family expenses
My program will implement array bounds checking while reading data. For example, say my MAX_ARRAY is set to 4, looking at the text file below, the “F” line (going into fam [] ) will reach it’s max at 555.55. At this point an error message will appear and not read any more data. On the other hand, the “O” line will continue because it hasn’t reached its max and no error will appear; that is, until it maxes out and outputs another error message.
The text file looks like the following:
TEXT FILE
O 111.11
F 2222.22
O 1333.33
O 0.00
F 1100.00
F 4444.44
F 555.55
F 0.00
F 898.89
F 7865.77
O 345.45
The following is my issue:
The program works perfectly if I set my MAX_ARRAY to cover all the data in the text file, but if I set it to where both of the arrays will max out at some point, only the family error message will display not the employee error message. Another weird thing that is happening is when I set MAX_ARRAY to 4 the complier gives me an error message: array2.exe has stopped working. This is proceeded by outputting the family error message. I’m confused at this point, I can’t see where the problem lies. Please help.

This will be a fairly large code(at least to me it is), Here is the 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
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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;


// Global constants
const string EMP_NAME = "employee";
const string FAM_NAME = "family";
const char EMPLOYEE = 'O';
const char FAMILY = 'F';
const int MAX_ARRAY = 4;
const string IN_MED_FILE  = "medical.txt";
const string OUT_MED_FILE = "medData.txt";

// prototypes
void readMedicalFile (ifstream&, double [], double [], int&, int&);	
double averageExpense (double [], int&);
double highestExpense (double [], int&);
double aboveAverageExpense (double, double [], int&);
void displayExpenses (double, double, double, string, int&);

//*************************************************************************
//  FUNCTION:	  main
//************************************************************************* 
int main()	
{
	//VARIABLES
	int cnt = 0;								// counter used for employee coverage
	int cnt1 = 0;								// second counter used for family coverage
	double empAverage;							// return value from the called function averageExpense for employee average
	double famAverage;  						// return value from the called function averageExpense for family average
	double empHighest;							// return value from the called function highestExpense for employee average
	double famHighest;							// return value from the called function highestExpense for family average
	double empAbove;							// return value from the called function aboveAverageExpense for employee average
	double famAbove;							// return value from the called function aboveAverageExpense for family average
	// opening the text file medical.txt
	ifstream inMedicalData;						
	inMedicalData.open(IN_MED_FILE.c_str());
	
	// array's
	double emp [MAX_ARRAY];
	double fam [MAX_ARRAY];
	
  	// verification that file exists 
    if (!inMedicalData)
    {	   
       cout << "The input data file does not exist!" << endl;
       cout << "Please verify input file and run the program again!" << endl;
       return 5;
    }
	// calling different functions to compute and display the results   	
	else
	{
		cout << "File opened successfully! Reading file." << endl << endl;
		readMedicalFile (inMedicalData, emp, fam, cnt, cnt1); 
		
		// functions to compute the average expenses
		empAverage = averageExpense (emp, cnt);
		famAverage = averageExpense (fam, cnt1);
		
		//functions to compute the highest expenses	
		empHighest = highestExpense (emp, cnt);
		famHighest = highestExpense (fam, cnt1);
		
		//functions to compute the above average number 
		empAbove = aboveAverageExpense(empAverage, emp, cnt);
		famAbove = aboveAverageExpense(famAverage, fam, cnt1);
		
		//functions to display the results
		displayExpenses (empAverage, empHighest, empAbove, EMP_NAME, cnt);
		cout << endl;
		displayExpenses (famAverage, famHighest, famAbove, FAM_NAME, cnt1);	
		

	}	
	return 0;
}
//*************************************************************************
//  FUNCTION:	 readMedicalFile
//*************************************************************************
void readMedicalFile (ifstream& inMedicalData, double emp [], double fam [], int& cnt, int& cnt1)
{
	char medStatus;							// temporarily store the first character in the file
	bool arrayFull = false;					// true if there is still space in the array

  while (inMedicalData >> medStatus)         	 
  {
	    if (medStatus == 'O' && !arrayFull)		//if first stored character is 'O' and array isn't full
	    {
	    	if(inMedicalData >> emp[cnt]) 		
	    	{
		    	cnt++;
		    	if (cnt > MAX_ARRAY)				   //testing if array is full
		    	{
	            	arrayFull = true;                  // set to exit loop
	            	cout << "Error -- too much data in file.  Only first "
	                << MAX_ARRAY << " employees will be used" << endl;
	        	}
	    	} 
		}		 
		else if (medStatus == 'F' && !arrayFull) 
		{
			inMedicalData >> fam[cnt1]; 
			cnt1++;
			if (cnt1 > MAX_ARRAY)					//testing if array is full 
	    	{
            	arrayFull = true;                  // set to exit loop
            	cout << "Error -- too much data in file.  Only first "
                << MAX_ARRAY << " families will be used" << endl;
        	}
		}	 
  }	
    inMedicalData.close();
 	
	return;	
}
//*************************************************************************
//  FUNCTION:	  averageExpense
//  DESCRIPTION:  Computes the average expenses for employee/family coverage
//				  and returns the the value
//*************************************************************************
double averageExpense (double avg [], int& count)
{
	double total = 0;
	double average;
	int num = 0;
	
	for (num = 0; num < count; num++)
        total += avg[num];

    average = total/count; 
    
    
    return average;
}
//*************************************************************************
//  FUNCTION:	  highestCharge
//  DESCRIPTION:  Computes the highest expenses for employee/family coverage
//				  and returns the the value
//*************************************************************************	
double highestExpense (double avg [], int& count)
{
	double highest = 0;
	int cell;
	
	for (cell = 0; cell <= count; cell++)
	{
		if (avg [cell] > highest)
			highest = avg [cell];
	}	
	return highest;
}
//*************************************************************************
//  FUNCTION:	  aboveAverageCharge
//  DESCRIPTION:  Computes value of a base number raised to an exponent
//*************************************************************************
double aboveAverageExpense (double average, double above [], int& count)
{
	double greater = 0;
	int cell;
	int num = 0;
	
	for (cell = 0; cell <= count; cell++)
	{
		if (above [cell] > average)
		{
			greater = above [cell];
			num++;
		}			
	}	
	return num;
}
//*************************************************************************
//  FUNCTION:	  displayExpenses
//  DESCRIPTION:  Displays the results for the average, highest, and 
//				  higher than average expenses for both 
//*************************************************************************
void displayExpenses (double average, double highest, double above, string classType, int& count)
{
	cout << fixed << showpoint << setprecision(2);
	cout << "For " << count << " employees with " << classType << " coverage:" << endl;
	cout << setw(31) << "Average expenses were $  " << average << endl;
	cout << setw(31) << "Highest expenses were $  " << highest << endl;
	cout << fixed << noshowpoint << setprecision(0);
	cout << setw(7) << above << " " << classType << "(s)" << " expenses were above the average" << endl;
	
	
	
	return;	
}
The problem is that you have two arrays but only one flag to say if they are full. You need two flags, one for each array.

Better yet, store the employee and family data in vectors instead. Vectors expand automatically to accomodate the data you put in them.

Your test for the whether you're exceeded the array size is off by one. The last valid index in the array is MAX_ARRAY-1, not MAX_ARRAY, so lines 96 and 108 should test if cnt >= MAX_ARRAY.
Thank you so much!! By adding that other flag, solved everything. Thanks for taking the time to look through my code and helping me see my mistakes :)
Topic archived. No new replies allowed.