I am looking to split my single-function C++ business calculator program into numerous functions. For example, separating the recurring parts of the code, such as when the program asks the user the costs of meals, fees, etc.
1. My goal is to not have any functions exceeding 20 lines.
My code:
#include
using namespace std;
int main(int argc, const char * argv[]) { //Command line arguments. 'argc' is argument count; it stores how many arguments that will be passed onto the main function of the program. 'argv' is argument vector' and it stores the array information. These are in this program as they are what pass the main values into the main function of the program.
int conti=0; //This line is for later on in the program when the user is asked whether or not they wish to use the calculator again, or quit. 'continue'
do{
std::cout
int daysOfTrip;float totalcost=0;
cin>>daysOfTrip;
int parkingfee[daysOfTrip],hotelFee[daysOfTrip-1]; //Minus one as hotel stays are one day shorter than a day trip. example: 3 day trip is 2 hotel nights.
int breaks[daysOfTrip],lunch[daysOfTrip],dinner[daysOfTrip]; //All mealtimes coincide with the days within the trip.
for (int i=0; i
breaks[i]=0;lunch[i]=0;dinner[i]=0;
}
int employeePaid = 0;
cout
float arrivalTime,deptTime,airfare,confOrSemRegFee;
cin>>deptTime;
cout
cin>>arrivalTime;
cout
cin>>airfare;
totalcost+=airfare;
cout
cin>>confOrSemRegFee;
totalcost+=confOrSemRegFee; //total cost equals 'total cost' plus 'conference or seminar fee'.
cout
int rentalOrTaxi=0;
do{
cout
cin>>rentalOrTaxi;
if(!(rentalOrTaxi==1 || rentalOrTaxi==2)){cout
}while(!(rentalOrTaxi==1 || rentalOrTaxi==2));
int typeOfCar=0;
int rentalFee=0;
float gasFee = 0;
if(rentalOrTaxi==1){
do{
cout
cin>>typeOfCar;
if(!(typeOfCar==1 || typeOfCar==2 || typeOfCar==3 || typeOfCar==4)) cout
}while(!(typeOfCar==1 || typeOfCar==2 || typeOfCar==3 || typeOfCar==4));
}
int milesDriven=0,totalTaxiCharges;
if(rentalOrTaxi==1){
cout
cin>>milesDriven;
if(typeOfCar==1){
rentalFee=20;
gasFee=0.24;
}else if(typeOfCar==2){
rentalFee=25;
gasFee = 0.27;
}else if (typeOfCar==3){
rentalFee=30;
gasFee = 0.32;
}else if(typeOfCar==4){
rentalFee=50;
gasFee = 0.45;
}
totalcost += rentalFee*daysOfTrip+gasFee*milesDriven;
}
else{
cout
cin>>totalTaxiCharges;
totalcost+=totalTaxiCharges;
}
if(rentalOrTaxi==1)
for (int i=0; i
cout cin>>parkingfee[i];
if(parkingfee[i]>10){//company cannot pay more than $10.
employeePaid = parkingfee[i]-10;
parkingfee[i]=10;
}
}
for (int i=0; i
cout cin>>hotelFee[i];
if(hotelFee[i]>90){//maximum 90$ reimbursement
employeePaid = hotelFee[i]-90;
hotelFee[i]=90;
}
}
int noOfBreakfasts=daysOfTrip, noOfLunches=daysOfTrip,noOfDinners=daysOfTrip;
for (int i=0; i
if(i==0){
if(deptTime>18){
cout
}else if(deptTime>12){
cout
cin>>dinner[i];
noOfBreakfasts–; //Post-decrement '–' for the program to run through the correct number of days of the user's trip.
noOfLunches–;
}else if(deptTime>7){
cout
cin>>lunch[i];
cout
cin>>dinner[i];
noOfBreakfasts–;
}
}else if(i==daysOfTrip-1){
if(arrivalTime
cout
noOfBreakfasts–;//he comes before 8 so breakfast is not provided, similarly below. The next chunk of lines is pretty self-explanatory.
noOfLunches–;
noOfDinners–;
}else if(arrivalTime
cout
cin>>breaks[i];
noOfLunches–;
noOfDinners–;
}else if(arrivalTime
cout
cin>>breaks[i];
cout
cin>>lunch[i];
noOfDinners–;
}else{
cout
cin>>breaks[i];
cout
cin>>lunch[i];
cout
cin>>dinner[i];
}
}else{
cout cin>>breaks[i];
cout
cin>>lunch[i];
cout
cin>>dinner[i];
}
}
for (int i=0; i
if(breaks[i]>9){ //the numbers represent the cost that the company will reimburse up to. For example, the company will reimburse breakfast up to $9.
employeePaid +=breaks[i]-9;
breaks[i]=9;
}
if(lunch[i]>12){
employeePaid +=lunch[i]-12;
lunch[i]=12;
}
if(dinner[i]>16){
employeePaid +=dinner[i]-16;
dinner[i]=16;
}
}
for (int i=0; i
totalcost+=breaks[i]+lunch[i]+dinner[i]+hotelFee[i]+parkingfee[i];
}
cout
cout
cout
cin>>conti;
}while(conti!=0);
return 0;
}