/* This program processes order forms, checking their arithmetic.

   Input (keyboard): Names of file containing the order forms.
   Input (file):     An order form.  Each file begins with the number
                       of transactions and the number of columns in
                       the order form.
   Output:           Instructions and prompts for the user, a copy of
                       each order, a list of the arithmetic errors in
                       each, and the total amount of the order.
------------------------------------------------------------------*/

#include <iostream.h>
#include <iomanip.h>

#include "Strings.h"
#include "Table.h"

int main(void)
{
   cout 
     << "\nThis program processes M X N order forms stored in files\n"
     << "\twhere M is the number of items ordered and N is the\n"
     << "\tnumber of columns on the form.  It assumes that each\n" 
     << "\tfile begins with the numbers M and N.\n";

   Strings
      FName;

   enum {                                   // The column headings:
          Transaction,                      //   0
          ItemCode,                         //   1
          UnitCost,                         //   2
          NumItems,                         //   3
          ItemTotal};                       //   4


   TableElement
      TransactionTotal;

   cout
      << "\nPlease enter the name of an order file (QUIT to quit): ";
   cin >> FName;

   while (FName != "QUIT")
   {
      Table
         OrderForm;

      OrderForm.Load(FName);

      cout << "\nProcessing each transaction in the order:\n";

      for (int r = 0; r < OrderForm.NumberOfRows(); r++)
      {
            cout << setiosflags(ios::fixed)
                 << resetiosflags(ios::showpoint)
                 << setprecision(2)
                 << OrderForm[r][Transaction] << setw(7)
                 << OrderForm[r][ItemCode]
                 << setiosflags(ios::showpoint)
                 << setw(8) << OrderForm[r][UnitCost]
                 << resetiosflags(ios::showpoint)
                 << setw(3) << OrderForm[r][NumItems]
                 << setiosflags(ios::showpoint)
                 << setw(8) << OrderForm[r][ItemTotal] << '\n';

         TransactionTotal = OrderForm[r][UnitCost]
                          * OrderForm[r][NumItems];

         if (TransactionTotal != OrderForm[r][ItemTotal])
         {
            cout << "*** Error in the preceding transaction:"
                 << "Item Total should be $"
                 << setiosflags(ios::showpoint | ios::fixed)
                 << setprecision(2)
                 << TransactionTotal << "\n\n";
            OrderForm[r][ItemTotal] = TransactionTotal;
         }
      }

      OrderForm.Write(FName);

      cout << "The correct total charge for this order is: $"
           << OrderForm.ColumnSum(ItemTotal) << "\n\n";

      cout << "\n\nPlease enter the name of an order file (QUIT to quit): ";
      cin >> FName;

   }
   return 0;
}
