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;
}
|