Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- using namespace std;
- #if moot
- int main()
- {
- int retCode=0;
- double darray[30];
- int readData(char[], double[], int);
- void displayDataTable(double[], int);
- retCode = readData("sum.txt", darray, 10);
- cout << "Return code is :" << hex << retCode << endl;
- displayDataTable(darray, retCode&0xFF);
- cin.get();
- return 0;
- }
- #endif
- ////////////////////// readData function////////////////////
- ////////////////////////////////////////////////////////////
- int readData(char filename[], double array[], int maxTriplets)
- {
- const int BAD_FILE = 0x0100; // Error: Input file does not exist
- const int EMPTY_FILE = 0x0200; // Input file exists, but is empty
- const int TOO_FEW_TRP = 0x0400; // Input file has less than MAX triplets
- const int EXACT_TRIPS = 0x0800; // Input file has exactly MAX triplets
- const int TOO_MNY_TRP = 0x1000; // Input file has more than MAX triplets
- const int EOF_REACHED = 0x2000; // Error: incomplete number triplet; EOF reached in middle of triplet
- const int INPUT_FAIL = 0x4000; // Error: A value was attempted to be read in, but input failed
- const int BAD_RANGE = 0x8000; // Error: Input value not within proper range.
- bool quitting = false;
- bool tripletIsGood; // Have 3 sets of data successfully been retrieved?
- double triplet[3]; // Array to hold triplets while validating
- int arrayStartingPoint = 0; // Index to start writing triplets to array
- int numberOfTriplets = 0;
- int errorMask = 0;
- int readIndex; // Index for reading and writing triplets to temporary array
- ifstream inputFile;
- // Open the file
- inputFile.open(filename);
- // Check if the file opened succesfully
- if (inputFile.fail())
- {
- errorMask |= BAD_FILE;
- // cout << "File didn't open successfully\n";
- }
- else
- // cout << "File opened successfully\n";
- inputFile >> triplet[0];
- if (inputFile.eof())
- {
- // cout << "File is empty\n";
- errorMask |= EMPTY_FILE;
- }
- do
- {
- // Read triplets from input file 3 at a time and store in an an array triplet.
- // if the operation succeeds for 3 values tripletIsGood becomes true.
- tripletIsGood = true;
- for (readIndex = 1; readIndex < 3 && tripletIsGood; readIndex++)
- {
- inputFile >> triplet[readIndex];
- // Check if EOF is reached in the middle of a triplet
- if (inputFile.eof())
- {
- inputFile.clear();
- errorMask |= EOF_REACHED;
- tripletIsGood = false;
- }
- // Check if inputFile >> failed..
- if (inputFile.fail())
- {
- inputFile.clear();
- errorMask |= INPUT_FAIL;
- tripletIsGood = false;
- }
- // Check if each value in the triplet is within valid range
- // What is the range anyway??
- if (triplet[readIndex] < 0.0 || triplet[readIndex] > 99.99)
- {
- errorMask |= BAD_RANGE;
- tripletIsGood = false;
- }
- // This will execute when 3 values have been read succesfully,
- if (readIndex == 2 && tripletIsGood)
- {
- tripletIsGood = true; // Set a flag to continue to write to the
- // array in the loop below
- numberOfTriplets++; // Add one to the number of triplets
- // successfully read
- cout << "There are " << numberOfTriplets << " triplets so far..."
- << endl << "maxTriplets is equal to " << maxTriplets << endl;;
- }
- }
- if (!(tripletIsGood))
- quitting = true;
- if (tripletIsGood && numberOfTriplets <= maxTriplets)
- // Read the values from triplet[] into the array starting from where it last left off.
- {
- for (int localIndex = 0; localIndex < 2; localIndex++)
- {
- array[arrayStartingPoint++] = triplet[localIndex];
- }
- }
- // Enter first number from triplet to check for EOF without returning an error
- inputFile >> triplet[0];
- if (inputFile.eof())
- quitting = true;
- // We must test for the same conditions we test the other 2 numbers from the triplet
- if (inputFile.fail())
- {
- errorMask |= INPUT_FAIL;
- quitting = true;
- }
- if (triplet[0] < 0 || triplet [0] > 9999)
- {
- errorMask |= BAD_RANGE;
- quitting = true;
- }
- // cout << "LANDMARK 2\n";
- if (numberOfTriplets == maxTriplets)
- quitting = true;
- // End of loop
- } while (!(quitting));
- if ((maxTriplets - numberOfTriplets) == 0)
- errorMask |= EXACT_TRIPS;
- else if ((maxTriplets - numberOfTriplets) > 0)
- TOO_FEW_TRP;
- else
- TOO_MNY_TRP;
- inputFile.close();
- return(errorMask + numberOfTriplets);
- }
- ////////////////////////////////////////////////////////////
- //////////////displayDataTable function/////////////////////
- ////////////////////////////////////////////////////////////
- void displayDataTable(double array[], int numTriplets)
- {
- cout << setw(10) << right << "Value1";
- cout << setw(10) << right << "Value2";
- cout << setw(10) << right << "Value3";
- cout << setw(9) << right << "Sum";
- cout << setw(18) << right << "Running Total";
- cout << endl;
- cout << setw(10) << right << "======";
- cout << setw(10) << right << "======";
- cout << setw(10) << right << "======";
- cout << setw(9) << right << "===";
- cout << setw(18) << right << "=============";
- cout << endl;
- int element = 0;
- double sum = 0;
- double runningTotal = 0;
- double tempArray[3];
- for (int count = 0; count < numTriplets; count++)
- {
- for (int n = 0; n < 3; n++)
- {
- tempArray[n] = array[element++];
- }
- cout << setw(10) << right << fixed << setprecision(2) << tempArray[0];
- cout << setw(10) << right << fixed << setprecision(2) << tempArray[1];
- cout << setw(10) << right << fixed << setprecision(2) << tempArray[2];
- sum = (tempArray[0] + tempArray[1] + tempArray[2]);
- cout << setw(9) << right << fixed << setprecision(2) << sum;
- runningTotal += sum;
- cout << setw(10) << right << fixed << setprecision(2) << runningTotal;
- cout << endl;
- }
- }
- /////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement