Advertisement
totesmuhgoats

exercise 5 -- cleaner still

Oct 13th, 2011
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. #if moot
  7. int main()
  8. {
  9.     int retCode=0;
  10.     double darray[30];
  11.    
  12.     int readData(char[], double[], int);
  13.     void displayDataTable(double[], int);
  14.  
  15.     retCode = readData("sum.txt", darray, 10);
  16.     cout << "Return code is :" << hex << retCode << endl;
  17.     displayDataTable(darray, retCode&0xFF);
  18.  
  19.     cin.get();
  20.  
  21.     return 0;
  22. }
  23. #endif
  24.  
  25. ////////////////////// readData function////////////////////
  26. ////////////////////////////////////////////////////////////
  27. int readData(char filename[], double array[], int maxTriplets)
  28. {
  29.  
  30.     const int BAD_FILE    = 0x0100; // Error: Input file does not exist
  31.     const int EMPTY_FILE  = 0x0200; // Input file exists, but is empty
  32.     const int TOO_FEW_TRP = 0x0400; // Input file has less than MAX triplets
  33.     const int EXACT_TRIPS = 0x0800; // Input file has exactly MAX triplets
  34.     const int TOO_MNY_TRP = 0x1000; // Input file has more than MAX triplets
  35.     const int EOF_REACHED = 0x2000; // Error: incomplete number triplet; EOF reached in middle of triplet
  36.     const int INPUT_FAIL  = 0x4000; // Error: A value was attempted to be read in, but input failed
  37.     const int BAD_RANGE   = 0x8000; // Error: Input value not within proper range.
  38.  
  39.  
  40.     bool quitting = false;
  41.     bool tripletIsGood;             // Have 3 sets of data successfully been retrieved?
  42.  
  43.     double triplet[3];              // Array to hold triplets while validating
  44.  
  45.     int arrayStartingPoint = 0;     // Index to start writing triplets to array
  46.     int numberOfTriplets = 0;
  47.     int errorMask = 0;
  48.     int readIndex;                 // Index for reading and writing triplets to temporary array
  49.     ifstream inputFile;
  50.    
  51.     // Open the file
  52.     inputFile.open(filename);
  53.  
  54.     // Check if the file opened succesfully
  55.     if (inputFile.fail())
  56.     {
  57.         errorMask |= BAD_FILE;
  58.        
  59. //      cout << "File didn't open successfully\n";
  60.     }
  61.     else
  62. //        cout << "File opened successfully\n";
  63.  
  64.     inputFile >> triplet[0];
  65.     if (inputFile.eof())
  66.     {
  67. //     cout << "File is empty\n";    
  68.        errorMask |= EMPTY_FILE;
  69.     }
  70.  
  71.     do
  72.         {
  73.         // Read triplets from input file 3 at a time and store in an an array triplet.
  74.         // if the operation succeeds for 3 values tripletIsGood becomes true.
  75.         tripletIsGood = true;
  76.  
  77.             for (readIndex = 1; readIndex < 3 && tripletIsGood; readIndex++)
  78.             {
  79.                 inputFile >> triplet[readIndex];
  80.                 // Check if EOF is reached in the middle of a triplet
  81.                if (inputFile.eof())
  82.                {
  83.                 inputFile.clear();
  84.                 errorMask |= EOF_REACHED;
  85.                 tripletIsGood = false;
  86.                 }
  87.                 // Check if inputFile >> failed..
  88.                 if (inputFile.fail())
  89.                 {
  90.                 inputFile.clear();
  91.                 errorMask |= INPUT_FAIL;
  92.                 tripletIsGood = false;
  93.                 }
  94.                 // Check if each value in the triplet is within valid range
  95.                 // What is the range anyway??
  96.                 if (triplet[readIndex] < 0.0  ||  triplet[readIndex] > 99.99)
  97.                 {
  98.                     errorMask |= BAD_RANGE;
  99.                     tripletIsGood = false;
  100.                 }
  101.                
  102.                 // This will execute when 3 values have been read succesfully,
  103.                 if (readIndex == 2 && tripletIsGood)
  104.                 {
  105.                 tripletIsGood = true;   // Set a flag to continue to write to the
  106.                                         // array in the loop below
  107.                 numberOfTriplets++;     // Add one to the number of triplets
  108.                                         // successfully read
  109.                 cout << "There are " << numberOfTriplets << " triplets so far..."
  110.                 << endl << "maxTriplets is equal to " << maxTriplets << endl;;
  111.                 }
  112.             }
  113.                
  114.         if (!(tripletIsGood))
  115.         quitting = true;
  116.  
  117.         if (tripletIsGood && numberOfTriplets <= maxTriplets)
  118.         // Read the values from triplet[] into the array starting from where it last left off.
  119.         {
  120.             for (int localIndex = 0; localIndex < 2; localIndex++)
  121.             {
  122.                 array[arrayStartingPoint++] = triplet[localIndex];
  123.             }
  124.         }
  125.        
  126.         // Enter first number from triplet to check for EOF without returning an error
  127.         inputFile >> triplet[0];
  128.        
  129.         if (inputFile.eof())
  130.         quitting = true;
  131.        
  132.         // We must test for the same conditions we test the other 2 numbers from the triplet
  133.         if (inputFile.fail())
  134.         {
  135.         errorMask |= INPUT_FAIL;
  136.         quitting = true;
  137.         }
  138.         if (triplet[0] < 0 || triplet [0] > 9999)
  139.         {
  140.         errorMask |= BAD_RANGE;
  141.         quitting = true;
  142.         }
  143.        
  144.        
  145. //      cout << "LANDMARK 2\n";
  146.        
  147.         if (numberOfTriplets == maxTriplets)
  148.             quitting = true;
  149.  
  150.     // End of loop
  151.     } while (!(quitting));
  152.    
  153. if ((maxTriplets - numberOfTriplets) == 0)
  154.    errorMask |= EXACT_TRIPS;
  155. else if ((maxTriplets - numberOfTriplets) > 0)
  156.      TOO_FEW_TRP;
  157. else
  158.     TOO_MNY_TRP;
  159.  
  160.        
  161.     inputFile.close();
  162.     return(errorMask + numberOfTriplets);
  163. }
  164.  
  165. ////////////////////////////////////////////////////////////
  166.  
  167. //////////////displayDataTable function/////////////////////
  168. ////////////////////////////////////////////////////////////
  169. void displayDataTable(double array[], int numTriplets)
  170. {
  171.     cout << setw(10) << right << "Value1";
  172.     cout << setw(10) << right << "Value2";
  173.     cout << setw(10) << right << "Value3";
  174.     cout << setw(9) << right << "Sum";
  175.     cout << setw(18) << right << "Running Total";
  176.     cout << endl;
  177.  
  178.     cout << setw(10) << right << "======";
  179.     cout << setw(10) << right << "======";
  180.     cout << setw(10) << right << "======";
  181.     cout << setw(9) << right << "===";
  182.     cout << setw(18) << right << "=============";
  183.     cout << endl;
  184.  
  185.     int element = 0;
  186.     double sum = 0;
  187.     double runningTotal = 0;
  188.     double tempArray[3];
  189.     for (int count = 0; count < numTriplets; count++)
  190.     {
  191.         for (int n = 0; n < 3; n++)
  192.         {
  193.         tempArray[n] = array[element++];
  194.         }
  195.     cout << setw(10) << right << fixed << setprecision(2) << tempArray[0];
  196.     cout << setw(10) << right << fixed << setprecision(2) << tempArray[1];
  197.     cout << setw(10) << right << fixed << setprecision(2) << tempArray[2];
  198.    
  199.     sum = (tempArray[0] + tempArray[1] + tempArray[2]);
  200.    
  201.     cout << setw(9) << right << fixed << setprecision(2) << sum;
  202.    
  203.     runningTotal += sum;
  204.    
  205.     cout << setw(10) << right << fixed << setprecision(2) << runningTotal;
  206.     cout << endl;
  207.     }
  208. }
  209.  
  210. /////////////////////////////////////////////////////////////
  211.  
  212.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement