reading a text file into parrellel arrays

Hello everyone, I managed to get this part of the program to work properly, but it seems to me that I could go about it a better way. The text file looks like the following:
O 111.11
F 2222.22
O 1333.33
The goal is to read the first character and place it into the correct array. Employee (O) array is emp[] and family (F) array is fam[]. I hope I explained it well enough.
Any suggestions would be greatly appreciated. I'll probably have more questions as I continue to develop this program. Thank you.

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
  #include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;


// Global constants
const char EMPLOYEE = 'O';
const char FAMILY = 'F';
const int MAX_ARRAY = 1000;
const string IN_MED_FILE  = "medical.txt";
const string OUT_MED_FILE = "medData.txt";

// prototypes
void display_header();
void readMedicalFile (ifstream&, double [], double []);	
double averageCharge (double [], int&);
double highestCharge (double [], int&);
double aboveAverageCharge;
double displayExpenses;

//*************************************************************************
//  FUNCTION:	  main
//  DESCRIPTION:  Asks user for a base number, then calls functions to read 
//                an exponent and compute the base raised to the exponent, and  
//                then displays the results.  Loops, requesting numbers until 
//                user enters zero for base.
//  OUTPUT:       Return Value:  0 on success
//  CALLS TO:	  GetPositiveNum, CalcAnswer
//************************************************************************* 
int main()	
{
	//VARIABLES
	int counter = 0;
	int cell;
	ifstream inMedicalData;		
	inMedicalData.open(IN_MED_FILE.c_str());
	
	// array's
	double emp [MAX_ARRAY];
	double fam [MAX_ARRAY];
	
	// display header
	display_header();
  	// 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;
    }   	
	else
	{
		cout << "File opened successfully! Reading file." << endl;
		readMedicalFile (inMedicalData, emp, fam); 	
		
		// Test to see if the data is accurately placed in the arrays (not part of program, just a test)	
		for (int num =0; num < 6; num++)
		{
			cout << "Employee expenses" << "(" << num << "): " << emp[num] << endl;
			cout << "Family expenses" << "(" << num << "):   " << fam[num] << endl << endl;
		}
	}

	return 0;
}

//*************************************************************************
//  FUNCTION:	 display_header
//  DESCRIPTION: displays the program header
//  INPUT:       n/a
//  OUTPUT: 	 n/a
//*************************************************************************
void display_header()
{
     cout << endl;
     cout << "######################################################################" << endl;
     cout << "This program will determine the average and highest expenses, and the " << endl;
     cout << "number of expenses higher than the average expenses, for all employees" << endl;
     cout << "under each health care coverage plan and display the results." << endl;
     cout << "The program will access a file called \"medical.txt \", the file will" << endl;
     cout << "contain an 'O' for employee only or an 'F' for family, followed by the" << endl;
     cout << "out-of-pocket expenses." << endl;
     cout << "######################################################################" << endl;
     cout << endl << endl; 
}

//*************************************************************************
//  FUNCTION:	 readMedicalFile
//  DESCRIPTION: Reads a positive number from the user
//  INPUT:       Parameter:    description - of value to read from user
//  OUTPUT: 	 Return value: num - validated positive number
//*************************************************************************
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' && cnt < MAX_ARRAY)
	    	{
	    		inMedicalData >> emp[cnt];
	    		cnt++;
			}		 
			else 
			{
				inMedicalData >> fam[cnt1]; 
				cnt1++;
			}	 
	  
	        // try to read data on next line
	        inMedicalData >> medStatus;
		}	 	
  }	
    inMedicalData.close();
 	
	return;	
}
Last edited on
Short description in what you need:
1) Maintain a counter alongside each array denoting number of actual entries in it. You will need in both your read function and outside of it (to know how much to proceed).
2) In your read function:
    a) Read both character and value simultaneously
    b) If read failed then file already ended and we need to stop our read function
    с) Else save value to array denoted by character and increment corresponding array counter.
Last edited on
Thank you for pointing me in the right direction. This helps me to see what I need and don't need. Thank you very much for your help :)
Topic archived. No new replies allowed.