#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
// function prototypes
int OpenAndValidateInputFile (ifstream& textIn);
void ReadAndStoreInputFile (ifstream& textIn, char unitType, double utilityCharge);
int main ()
{
//
constint 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...
// if not, print a warning and exit program from main with a return code 1
That return 1; won't change main()'s error code. So if you want to terminate the program at that point, use exit(1); instead.
Now, about the arrays. To me it appears that you want to store floating point values, not characters. You discard the R's and B's you read, you only need them to figure out in which array to put the values that follow.
So is this what you want?
1 2 3
// float or double, the latter uses double the memory but is more accurate
float ResidentialUnit [MAX];
float BusinessUnit [MAX];
Then you need a counter for both arrays, to know the current position where to insert the data.
You increase the correct counter every time you add data to the array it describes.
And also check if the counter doesn't exceed the array's MAX limit.
Now, I don't know if using arrays is a requirement of this project, but if not a lot of hassle can be avoided by using std::vector. http://cplusplus.com/reference/stl/vector/
I do have to use Array as a requirement for this project.
As for the char/float issue, you both are correct...i did think about that incorrectly and fr some reason was trying to store the R's...not the utility charge. Dumb.
The exit/return...I am NOT allowed to use "exit" I have to use return, but here are the instructions, I thought it was correct..but you may have helped me find an error :)
"When the program tries to open the data file, if the data file does not exist, the program should issue an error
message, and return from the main function with a return code of 1 (NOTE: This means that if the program
attempts to open the file from within another function, it will need to pass back information about the success or
failure to main, so main can exit properly)."
... and now I see OpenAndValidateInputFile() is missing a return statement outside the if(). (This should pop up as an error the moment you try to compile your program.)
OK so if you can't use exit() you can just check what OpenAndValidateInputFile() returns in an if() inside main():
1 2
if (OpenAndValidateInputFile (textIn) == 1) // assuming it returns 0 for all OK
return 1; // this exits main() as expected
I am using VS Express 2010, it isn't having any issues compiling with the return statement as it is. Are you saying I need a return 1; where it is in the if statement if the file doesn't open, and ANOTHER one outside of the statement??
Hmmm...ill have to mess around with that a little bit and see if it makes sense to me. This is my first week even learning what an array is in C++ :/ CONFUSED when trying to figure out to pass through user defined functions.