1) Code changed to ISO C++.
2) Formatting changed to ANSI style.
3) Added colored syntax.
4) Formatted to line-break at max 80 characters.
Code:
#include <iomanip>
#include <iostream>
#include <cstring> //old style string handling
using namespace std; //for brevity
//class cruiser template
class cruiser
{
private:
char regNo[50];
double engpower;
char salesmanID[60] ;
double importcost;
double sellingprice;
public:
static int numcommercial;
static int numprivate;
void setCruiser(char* ireg, double iengp, char* isalesman,
double iimportcost, double isellprice)
{
strcpy(regNo, ireg);
engpower = iengp;
strcpy(salesmanID, isalesman);
importcost = iimportcost;
sellingprice = isellprice;
}
void displaycruiser()
{
cout << regNo << "\t" << salesmanID << "\t" << sellingprice ;
}
char* get_regNo(void){ return regNo;}
}; // end of cruiser class
//---------------------------------------------------------------------
class Private : public cruiser
{
private:
double yearbuilt;
double importtax;
double ownershipfee;
public:
void setPrivate(double xyearbuilt, double ximporttax,double xownerfee,
char* ireg, double iengp, char* isalesman,
double iimportcost, double isellprice)
{
yearbuilt = xyearbuilt;
importtax = ximporttax;
ownershipfee = xownerfee;
void setCruiser(char*ireg,double iengp, char *isalesman,
double iimportcost, double isellprice);
}
void Private::displaypte()
{
displaycruiser();
cout << "\t" << "private" << "\t" << importtax << "\t"
<< ownershipfee << endl;
}
};
// Class Template for Commercial -----------------------------------------
class Commercial : public cruiser
{
private:
double maxloadcapacity;
int numengines;
double regfees;
public:
void setCommercial(double xmaxloadcapacity, int xnumengines,
double xregfees, char* ireg, double iengp, char* isalesman,
double iimportcost, double isellprice)
{
maxloadcapacity = xmaxloadcapacity;
numengines = xnumengines;
regfees= xregfees;
setCruiser(ireg, iengp, isalesman, iimportcost, isellprice);
}
void Commercial::displaycc()
{
displaycruiser();
cout << "\t" << "commercial" << "\t"
<< numengines << "\t" << regfees << endl;
}
};
//------------------------------------------------------------------------
int cruiser::numcommercial = 0;
int cruiser::numprivate = 0;
int main()
{
int g,j,num,type, choice;
char xregNo[30];
double xengpower;
char xsalesmanID[30] ;
double ximportcost;
double xsellingprice;
double xyearbuilt;
double ximporttax;
double xownerfee;
double xmaxloadcapacity;
int xnumengines, i=0;
double xregfees;
double comA, comB;
double totalcount = 0;
Private pte[100];
Commercial cc[100];
while (choice != 5) // <<---- choice never initialized
{
cout << endl;
cout << "1. Add a new sales transaction" << endl;
cout << "2. Update an existing transaction" << endl;
cout << "3. Generate Sales Transaction Report" << endl;
cout << "4. Generate Sales Summary" << endl;
cout << "5. Exit" << endl;
cout << "Please choose: ";
cin >> choice;
cin.ignore(1);
switch (choice)
{
case 1 :
cout << "Enter type of vehicle: 1-Private 2-Commercial";
cin >> type; // type = 1 = private | type = 2 = commercial.
cin.ignore(1);
if (type == 1)
{
cout << "Enter Registration No:";
cin >> xregNo;
cout << "Enter Engine Power:";
cin >> xengpower;
cout << "Enter salesman ID:";
cout << "Enter Import cost No:";
cin >> ximportcost;
cout << "Enter selling price:";
cin >>xsellingprice;
cout << "Enter year of built:";
cin >>xyearbuilt;
cout << "Enter ownership fee:";
cin >>xownerfee;
//prompt for yearbuilt, ownership fee and store in variables
//declared in the main (above).
//based on importcost and yearbuilt, calculate final import tax
//(see question paper for the conditions
pte[cruiser::numprivate].setPrivate(xyearbuilt,
ximporttax, xownerfee,xregNo, xengpower, xsalesmanID,
ximportcost, xsellingprice); //<< ----- ximporttax never initialized
for (int g =0 ; g < cruiser::numprivate; g++);
{
// add 1 to numprivate.
}
}
else
{
cout << "Enter Registration No:";
cin >> xregNo;
cout << "Enter Engine Power:";
cin >> xengpower;
cout << "Enter salesman ID:";
cin >> xsalesmanID;
cout << "Enter Import cost No:";
cin >> ximportcost;
cout << "Enter selling price:";
cin >>xsellingprice;
cout << "Enter maximum loading capacity:";
cin >>xmaxloadcapacity;
cout << "Enter num of engines:";
cin >>xnumengines;
//prompt for all the Cruiser's (parent class) attributes and
//store in variables declared in the main (above).
//prompt for max load capcity, number of engines
//based on number of engines and import cost, calculate the
//registration fees.
//Remember that registration fee has 2 components, comA and comB.
xregfees = comA + comB; //comA & comB never initialized
cc[cruiser::numcommercial].setCommercial(xmaxloadcapacity,
xnumengines, xregfees, xregNo, xengpower, xsalesmanID,
ximportcost, xsellingprice);
for (int j =0 ; j < cruiser::numcommercial; j++);
{
// add 1 to numcommercial.
}
}
break;
case 2: //edit
cout << "Select (1) Private or (2) Commercial to edit";
cin >>type;
cin.ignore(1);
if (type == 1)
{
//this will display out all the registration number of
//private cruisers.
for (int y =0 ; y < cruiser::numprivate; y++)
{
cout << "("<< y << ")" << pte[y].get_regNo() << endl;
}
// user will then select the index for update.
cout << "select number to update; 0-" << cruiser::numprivate;
cin >>num;
//-------------------------------------
//prompt for all the Cruiser's (parent class) attributes.
//----------------------------------
//prompt for yearbuilt, ownership fee and store in
//variables declared in the main (above).
// based on importcost and yearbuilt, calculate final import tax
pte[num].setPrivate(xyearbuilt, ximporttax, xownerfee,xregNo,
xengpower, xsalesmanID, ximportcost, xsellingprice);
}
else //type =2 commercial selected
{
for (int t =0 ; t < cruiser::numcommercial; t++)
cout << "("<< t << ")" << cc[t].get_regNo() << endl;
cout << "select number to update; 0-" << cruiser::numcommercial;
cin >>num;
// based on number of engines and import cost,
//calculate the registration fees (see question
//paper for //conditions. remember that registration
//fee has 2 components, comA and comB.
xregfees = comA + comB;
cc[num].setCommercial(xmaxloadcapacity, xnumengines,
xregfees,xregNo, xengpower, xsalesmanID,ximportcost,
xsellingprice);
}
break;
case 3:
cout << " TOMCAT CRUISER DEALING CO. PTE LTD " << endl;
cout << " SALES TRANSACTIONS Report " << endl;
cout << " Reg.No| SalesmanID |Sprice |ImportCost |" ;
cout << "Import tax/num eng|fees" ;
cout << endl << endl;
if (cruiser::numprivate > 0)
{
for (int g =0 ; g < cruiser::numprivate; g++)
{
pte[g].displaypte();
}
//use for loop can call the displaypte function,
//to display out each object of the pte array.
//--->>> //cout << "("<< g << ")" << pte[g].displaypte()<< endl;
}
if (cruiser::numcommercial > 0)
{
//use for loop can call the displaycc function,
//to display out each object of the cc array.
for (int j =0 ; j < cruiser::numcommercial; j++)
{
//--->>> //cout << "("<< j << ")" << cc[j].displaycc()<<endl;
}
}
cout << endl << endl;
cout << "# of Private Cruisers Sold : " << cruiser::numprivate;
cout << endl;
cout << "# of Commercial Cruisers Sold : " << cruiser::numcommercial;
break;
default:
cout << "Invalid choice." << endl;
} // end switch
} // end while
return 0;
}