stop reading data into parrellel arrays

Hello everyone, Here's my problem. Part of my program is to read a text file into parallel arrays. One array is for Employee expenses(O) and the other for Family(F). When one of the arrays maxes out my program is supposed to output an error message and exit out of the loop; the other array will continue until it maxes out and the exit.
I was going to use a bool variable and set it to false, when it maxes out it will be true and exit the loop, but I'm not sure where to put it. Can you help me figure this out? Thank you.

My function for reading into my arrays:
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
  void readMedicalFile (ifstream& inMedicalData, double emp [], double fam [])
{
	char medStatus;							// temporarily store the first character in the file
	bool arrayFull = false;					// true if there is still space in the array
	int cnt = 0;							// counter used in the if statement for "O"
	int cnt1 = 0;							// second counter used in the if statement for "F"
    int count; 								// counter used for the for statement to start the loop

  while (inMedicalData)         	// file opened successfully
  {
	  inMedicalData >> medStatus;   // read first character
	  for (count = 0; count < MAX_ARRAY; count++) 
	  {                
	    	if (medStatus == 'O')
	    	{
	    		inMedicalData >> emp[cnt];
	    		cnt++;
	    		if (cnt == MAX_ARRAY)
	    		 {
            		arrayFull = true;                  // set to exit loop
            		cout << "Error -- too much data in file.  Only first "
                 	<< MAX_ARRAY << " patients ages will be used" << endl;
        		}
			}		 
			else if (medStatus == 'F') 
			{
				inMedicalData >> fam[cnt1]; 
				cnt1++;
				if (cnt1 == MAX_ARRAY)
	    		 {
            		arrayFull = true;                  // set to exit loop
            		cout << "Error -- too much data in file.  Only first "
                 	<< MAX_ARRAY << " patients ages will be used" << endl;
        		}
			}	 
	  
	        // try to read data on next line
	        inMedicalData >> medStatus;
		}	 	
  }	
  	cout << emp[5];
    inMedicalData.close();
 	
	return;	
}
The expression inMedicalData >> medStatus indicates with a bool whether it was successfull. You can use the expression directly for while or if.
The loop on line 12 is unnecessary due to the use of arrayFull. So it can be rewritten as:
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
  void readMedicalFile (ifstream& inMedicalData, double emp [], double fam [])
{
	char medStatus;							// temporarily store the first character in the file
	bool arrayFull = false;					// true if there is still space in the array
	int cnt = 0;							// counter used in the if statement for "O"
	int cnt1 = 0;							// second counter used in the if statement for "F"

  while ((inMedicalData >> medStatus) && (! arrayFull))         	// file opened successfully
  {
	    	if (medStatus == 'O')
	    	{
	    		if(inMedicalData >> emp[cnt]) // Note
	    		{
	    		cnt++;
	    		if (cnt >= MAX_ARRY)
	    		 {
            		arrayFull = true;                  // set to exit loop
            		cout << "Error -- too much data in file.  Only first "
                 	<< MAX_ARRAY << " patients ages will be used" << endl;
        		}
	    		}
	    		else
            		cout << "Error -- reading medical data";
  
			}		 
			else if (medStatus == 'F') 
			{
				inMedicalData >> fam[cnt1]; // Note: you should check for success as shown above
				cnt1++;
				if (cnt1 >= MAX_ARRAY)
	    		 {
            		arrayFull = true;                  // set to exit loop
            		cout << "Error -- too much data in file.  Only first "
                 	<< MAX_ARRAY << " patients ages will be used" << endl;
        		}
			}	 
  }	
  	cout << emp[5];
    inMedicalData.close();
 	
	return;	
}
Not tested. Improve the indention
Thank you for your reply. I was wondering if that for loop should be there or not, my original program looked a little off. It makes it look and run smoother by getting rid of the extra loop. Now I can continue with the other parts of the program. I'll most likely, be adding more posts that continue along with this program. I really appreciate you helping me to look at my program in a different way. Thank you.
Topic archived. No new replies allowed.