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

//for logfile
struct log
{
    long number;
    char date[9];
    char time[9];
    char gate[2];
};

//for customers
struct customer
{
    long number;
    char initial[2];
    char lname[20];
    char snumber[7];
    char sname[17];
    char stype[17];
    char suburb[17];
    char state[4];
    char postcode[5];
};

//constants for array sizes
const int MAXLOGS=50;
const int MAXCUST=20;


// function protoytpes
int searchProcessed(long[],long,int);//searches already processed records
int find (customer[],long,int);      //searches customer array
void showHeadings();                 //show headings
void drawline();                     //draws line

int main ()
{
  //declare array to hold logs
  log logArray[MAXLOGS];

  //declare infile stream and link to log.txt
  ifstream in;
  in.open("log.txt");

  int numOfLogs = 0; // number of logs

  //load-up array with logs
  while(!in.eof())
  {
        in >> logArray[numOfLogs].number;
        in >> logArray[numOfLogs].date;
        in >> logArray[numOfLogs].time;
        in >>logArray[numOfLogs].gate;
        in >> ws;
        numOfLogs++;
  }
  in.close();

  //declare array of type customer
  customer customerArray[MAXCUST];
  //open the file
  in.open("customer.txt");


  int numberOfCustomers = 0; //number of customers

  //load-up array with customers
    while(!in.eof())
  {
        in >> customerArray[numberOfCustomers].number;
        in >> customerArray[numberOfCustomers].initial;
        in >> customerArray[numberOfCustomers].lname;
        in >> customerArray[numberOfCustomers].snumber;
        in >> customerArray[numberOfCustomers].sname;
        in >> customerArray[numberOfCustomers].stype;
        in >> customerArray[numberOfCustomers].suburb;
        in >> customerArray[numberOfCustomers].state;
        in >> customerArray[numberOfCustomers].postcode;
        in >> ws;
        numberOfCustomers++;

  }
  in.close();

  //declare array of type long, to hold custNumbers of
  //previously processed records
  long processed[MAXLOGS];
  //initialize to zero
  for (int i=0;i<MAXLOGS;i++)
  {
      processed[i]=0;
  }
  int processedSize=0;//nothing in array yet

  // required heading
  cout << "\t\tThis is the HEADING of the statement\n" << endl;

  //main loop to process records
  //processing the log array from first to number of logs
  for ( int z = 0; z < numOfLogs; z++ )
  {
        //if current customer number has not been previously processed
        if (searchProcessed(processed,logArray[z].number,numOfLogs) < 0)
        {
            //add customer number into array of previously processed records
            processed[processedSize++] = logArray[z].number;

            // find the position of the customer in customer array
            // by searching with current customer number from log array
            int position = find(customerArray,logArray[z].number,numberOfCustomers);

            //if customer is found
            if (position >=0)
            {
                drawline(); //draw a line

  cout << "\nEWAY BILL                                  E-way Motorway\n"
          << "                                         " << customerArray[position].number;
    cout << "\n \n"<<customerArray[position].lname <<customerArray[position].initial;
    cout << " \n"<< customerArray[position].snumber <<customerArray[position].sname <<customerArray[position].stype;
    cout << " \n"<<customerArray[position].suburb <<customerArray[position].state <<customerArray[position].postcode;
    cout << "E-way motor bill for: "<<customerArray[position].number << endl << endl;
    } // end printcustomerstatement

          double total = 0.0;//accumulator for totals
          showHeadings();    //show headings
          drawline();        //draw a line

          //from current position of log array to number of logs
          //trying to process every entry of customer in the log file
          for (int l = z ;l<numOfLogs;l++)
          {

              //if found an entry matching current customer number
              //(first entry is always processed).
              //processSize is -1 because it has been incremented above
              if(processed[processedSize-1] == logArray[l].number)
              {
                    //display detail line
                    cout <<setw(8) << logArray[l].date
                          <<setw(9)<< logArray[l].time
                          <<setw(8)<< logArray[l].gate;

                    //checking for toll-gate code
                    //adding to total for correct amount
                    //"case of" not used because 'gate' is of type char[]
                    //not int.
                    if(! strcmp(logArray[l].gate,"A") )
                    {
                        cout <<setw(7)<< "$2.20";
                        total = total + 2.20;
                    }
                    else if(! strcmp(logArray[l].gate,"B") )
                    {
                        cout <<setw(7)<< "$2.80";
                        total = total + 2.80;
                    }
                    else if(! strcmp(logArray[l].gate,"C") )
                    {
                        cout <<setw(7)<< "$3.30";
                        total = total + 3.30;
                    }
                    else
                    {
                        cout <<setw(7)<< "$3.80";
                        total = total + 3.80;
                    }
                    cout << endl;

                }

          }

    cout << endl << setw(8)<<"DATE" <<setw(9) <<"TIME"
          << setw(8) << "STATION"
          << setw(7) << "AMOUNT"
          <<endl;


// Function tollCharge

  cout << endl << endl;
  cout << "Total charge usage of E-way for:" << customerArray[position].number  <<" "
         << "                   $" << setiosflags(ios::fixed)<<setprecision(2)<< total <<endl<<endl;

    cout<<"\nEnd OF E-Way Statement";
    return 0;
}


// searches the array for number and returns position if found
//  else returns -1
int searchProcessed(long processed[], long num,int size)

{
  for (int i = 0; i < size;i++)
  {
        if (num == processed[i])
            return i;
  }
    return -1;
}


//searches customer array for number and returns position if found
//else returns -1
int find (customer customerArray[],long num ,int size)
{
  for (int i=0;i<size;i++)
  {
        if ( num == customerArray[i].number)
            return i;

  }
  return -1;
}
;
}
}

Edited 3 Years Ago by pyTony: fixed formating

and what's wrong? (ie. does it not compile, or does it not work as you think it should?)

Also add what your program is supposed to do(a general idea) and put the compiler out put in [ c o d e ]

This article has been dead for over six months. Start a new discussion instead.