Array help

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

// function prototypes
int OpenAndValidateInputFile (ifstream& textIn);
void ReadAndStoreInputFile (ifstream& textIn, char unitType, double utilityCharge);

int main ()
{
	//
	const int MAX = 500;										// maximum number of stored units in array

	// local variables
	ifstream textIn;											// input file stream variable
	char unitType = 0;											// unit type - 'R' residential or 'B' business
	double utilityCharge = 0;									// utility charge
	char ResidentialUnit [MAX];									// array for 'R' residential units
	char BusinessUnit [MAX];									// array for 'B' business units

	OpenAndValidateInputFile (textIn);							// function call

	ReadAndStoreInputFile (textIn, unitType, utilityCharge);	// function call

	system ("PAUSE");

}

int OpenAndValidateInputFile (ifstream& textIn)
{
    // open input file
    textIn.open ("UNITS.TXT");

	// check if file opens successfully
	// if not, print a warning and exit program from main with a return code 1
	if (!textIn.is_open ())
	{
		cout << "Failed to open input file UNITS.TXT" << endl;
		
		return 1;
	}
}

void ReadAndStoreInputFile (ifstream& textIn, char unitType, double utilityCharge)
{
	while (textIn.peek () != EOF)
	{
		textIn >> unitType;
		textIn >> utilityCharge;
	}

	if (unitType == 'R')
	{

}


Can anyone explain the process of storing units in an array?
As im sure you have noticed, I stopped the code at the if statement...
what I am attempting to do is open and validate a text file that contains an 'R' for residential units and a 'B' for business units, and next to the letter it has a utility charge..
i.e.
unitType
R 74.12
B 102.93
R 68.02
I am opening and validating, then reading the data line by line. What I need to do is if the unitType is R, it needs to be stored in the ResidentialUnit array, and if it is a B needs to be stored in the BusinessUnit array, for calculations later...

Any suggestions or explanations would be greatly appreciated!
I think what you intended to store in the respective arrays is the utility change.
If that is the case, the arrays need to be doubles, not chars.
You also need a count of how many of each you have.
1
2
3
4
  double ResidentialUnit [MAX];					
  double BusinessUnit [MAX];	
  int  ResidentialCount = 0;
  int  BusinessCount = 0;

Then simply store the charge in the respective array and increment the count:
1
2
3
4
5
6
7
8
9
10
if (unitType == 'R')
{  //  Check here if the size of the array has been exceeded
  ResidentialUnit[ResidentialCount] = utilityCharge;
  ResidentialCount++;  //  Increment the number of units
}
if (unitType =='B')
{  //  Check here if the size of the array has been exceeded
  BusinessUnit[BusinessCount] = utilityCharge;
  BusinesslCount++;  //  Increment the number of units
}




This thread was made obsolete by: http://cplusplus.com/forum/beginner/75299/
Topic archived. No new replies allowed.