Reading data from a text file into arrays

The program I'm working on is reading the data from a text file, and is supposed to read them into two separate arrays based off of what kind of information it is.
For this program, the text file has information formatted as such:

R 170.69
B 146.96
B 64.02

Whereas R stands for Residential and B stands for Business, and the price next to it correlates to that character.

I've just started learning how to do arrays, and I don't quite have a handle on them, or how I'm supposed to make the data go into its corresponding array. I'm also having a separate function for reading the file, and then another function actually passing the data into each array.

The below is my code so far:

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

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

const string FILENAME = "UNITS.txt";
const int MAX = 500;       //max amount of data for array

void readFile(ifstream& inData, char& type, float& utilityCharge);
void writeToArrays(ifstream& inData, char& type, float& utilityCharge, float businessCharge[], float residentialCharge[]);

int main()
{
   ifstream inData;
   
   char type;                            //Business or Residential
   float utilityCharge;
   float businessCharge[MAX] = {0.0}; 
   float residentialCharge[MAX] = {0.0}; 
  
   inData.open (FILENAME.c_str());       // open the file
  
   if (inData) {                         // if file opened successfully 
       readFile(inData, type, utilityCharge);
       inData.close();          // close the file
   }
   else
   {
       cout << "Error opening text file " << FILENAME << endl;
       return 1;
   }
       
    system("PAUSE");
    return 0;
}

void readFile(ifstream& inData, char& type, float& utilityCharge)
{    
     char ch; 
     
     while (inData){                            //while document is open
     do{
         inData >> type >> utilityCharge;       //Read values in .txt
         
         cout << type << " " << utilityCharge << " ";
         }while (inData && (ch != '\n'));       //Looks for new line
     }
     return;
}
     
void writeToArrays(ifstream& inData, char& type, float& utilityCharge, float businessCharge[], float residentialCharge[])
{
     int r1;           //counter for residential
     int b1;           //counter for business
     
     if (char type == 'R')
     {
         for (r1 = 0; r1 < MAX; r1++)
             inData >> residentialCharge[r1];
     }
     else if (char type == 'B')
     {
          for (b1 = 0; b1 < MAX; b1++)
             inData >> businessCharge[b1];
     }
     return;
}
     
Topic archived. No new replies allowed.