Assignment 3 Problem Travel Expenses using functions Write a
Assignment 3
 
 Problem Travel Expenses using functions:
 
 Write a program that calculates and displays the total travel expenses of a businessperson on a trip. The program should declare all variables in the main function and then call the functions to ask for and return the following:
 The total number of days spent on the trip
 The time of departure on the first day of the trip, and the time of arrival back home on the last day of the trip
 The amount of any round-trip airfare
 The amount of any car rentals
 Miles drive, if a private vehicle was used. Calculate the vehicle expense as $0.50 per km driven.
 Parking fees (The company allows up to $10 per day. Anything in excess of this must be paid by the employee.)
 Taxi fees, if a taxi was used anytime during the trip (The company allows up to $20 per day, for each day a taxi was used. Anything in excess of this must be paid by the employee.)
 Conference or seminar registration fees
 Hotel expenses (The company allows up to $90 per night for lodging. Anything in excess of this must be paid by the employee.)
 The amount of each meal eaten. On the first day of the trip, breakfast is allowed as an expense if the time of departure is before 7am. Lunch is allowed if the time of departure is before 12 noon. Dinner is allowed on the first day if the time of departure is before 6pm. On the last day of the trip, breakfast is allowed if the time of arrival is after 8am. Lunch is allowed if the time of arrival is after 1pm. Dinner is allowed on the last day if the time of arrival is after 7pm. The program should only ask for the amounts of allowable meals. (The company allows up to $9 for breakfast, $12 for lunch, and $16 for dinner. Anything in excess of this must be paid by the employee.)
 The program should calculate and display the total expenses incurred by the businessperson, the total allowable expenses for the trip, the excess that must be reimbursed by the businessperson, if any, and the amount saved by the businessperson if the expenses were under the total allowed.
 
 Input Validation: Do not accept negative numbers for any dollar amount or for miles driven in a private vehicle. Do not accept numbers less than 1 for the number of days. Only accept valid times for the time of departure and the time of arrival.
 
 What to hand in:
 Copy your source code into your Word document
 Several runs to show your program works with valid and invalid data
 A User Guide for the “businessperson”
 A hierarchy chart to show the relationship of your functions.
 
 
 
 Prototypes may look like the following:
 // Function prototypes
 int   daysSpent();
 void times(double &, double &);
 bool isValidTime(double);
 double airFare();
 double carRental();
 double vehicle();
 double parking(int);
 double taxi(int);
 double registration();
 double hotel(int);
 double meals(int, double, double);
 double getBreakfast();
 double getLunch();
 double getDinner();
 
 // Global variable to hold total expenses of employee
 double totalExpenses = 0;
 
 
 
 
 
 
 
 
 
 
 
 
Solution
code:
#include <iostream>
 #include <iomanip>
 #include<string>
 #include<sstream>
 using namespace std;
 int daysSpent();
 void times(double &, double &);
 bool isValidTime(double);
 double airFare();
 double carRental();
 double vehicle();
 double parking(int);
 double taxi(int);
 double registration();
 double hotel(int);
 double meals(int, double, double);
 double getBreakfast();
 double getLunch();
 double getDinner();
 double totalExpenses = 0;
 int main()
 {
    double allowable = 0;
    double time1;
    double time2;
    int days;
    days = daysSpent();
    times(time1, time2);
    allowable = airFare();
    allowable += carRental();
    allowable += vehicle();
    allowable += parking(days);
    allowable += taxi(days);
    allowable += registration();
    if (days > 1)
        allowable += hotel(days);
    allowable += meals(days, time1, time2);
    cout << fixed << showpoint << setprecision(2);
    cout << \"\ Total expenses: $\" << totalExpenses << endl;
    cout << \"\ Allowable expenses: $\" << allowable << endl;
    // Display amount to be reimbursed.
    if (totalExpenses > allowable)
        cout << \"Amount to be reimbursed: $\" << (totalExpenses - allowable);
    // Display amount saved.
    else if (totalExpenses < allowable)
        cout << \"Amount saved: $\" << (allowable - totalExpenses);
    cout << endl << endl; return 0;
 }
int daysSpent()
 {
    int days;
    cout << \"How many days were spent on the trip? \"; cin >> days;
    while (days < 1)
    {
        cout << \"Days must be one or more.\ \" << \"How many days were spent on the trip? \";
        cin >> days;
    }
    return days;
 }
 void times(double &start, double &end)
 {
    cout << \"Enter the arrival time (HH.MM): \";
    cin >> start;
    while (!isValidTime(start))
    {
        cout << \"Enter the arrival time (HH.MM): \";
        cin >> start;
    }
    cout << \"Enter the departure time (HH.MM): \";
    cin >> end;
    while (!isValidTime(end))
    {
        cout << \"Enter the departure time (HH.MM):\" ;
        cin >> end;
    }
 }
 bool isValidTime(double t)
 {
    bool status = true;
    ostringstream ss;
    ss<<t;
    string ti=ss.str();
    string tok;
    istringstream is(ti);
    getline(is, tok, \'.\');
    istringstream iss(tok[0]);
    int hour;
    iss>>hour;
    istringstream iss1(tok[1]);
    int min;
    iss>>min;
    if((hour>=0.00)&& (hour<=24.00))
    {
        if((min>=0) && (min<=60))
            return true;
        else
            return false;
    }
    else
        return false;
 }
      
 double airFare()
 {
    double fare;
    cout<<\"Enter the air fare\"<<endl;
    cin>>fare;
    while(fare<0)
 {
 cout<<\" Fare is not correct enter correct fare\ \";
 cin>>fare;
 }
    totalExpenses=totalExpenses+fare;
    return 0;
 }
double carRental()
 {
    int num_days;
 double rent;
 char ch ;
    double total_rent;
 cout<<\" Did you rent the car? Please enter y for Yes abd n for no\";
 cin>>ch;
 while((ch != \'y\') && (ch != \'n\'))
    {
 cout<<\"Please enter either y or n\";
 cin>>ch;
 }
 if(ch==\'y\')
 {
 cout<<\"Enter daily rent for car\ \";
 cin>>rent;
 cout<<\"Enter number of days rented the car?\ \";
 cin>> num_days;
 total_rent=rent*num_days;
 
 }
 else
 total_rent=0;
    totalExpenses=totalExpenses+total_rent;
    return 0;
 
 }
double vehicle()
 {
    char ch;
    double miles;
    double charge;
    cout<<\" Did you use the private vehicle? Please enter y for Yes abd n for no\";
 cin>>ch;
 while((ch != \'y\') && (ch != \'n\'))
    {
 cout<<\"Please enter either y or n\";
 cin>>ch;
 }
 if(ch==\'y\')
 {
 cout<<\"Enter miles driven in km\ \";
 cin>>miles;
 charge=miles*0.50;
 
 }
 else
 charge=0;
    totalExpenses=totalExpenses+charge;
    return 0;
 }
 double parking(int days)
 {
    double park;
    double total_charge;
    cout<<\" Enter expenses for total parkng\ \";
 cin>>park;
    totalExpenses=totalExpenses+park;
 return 10*days;
 }
 double taxi(int days)
 {
    double taxi;
    char ch;
    double total_charge;
    cout<<\" Did you use taxi? Please enter y for Yes abd n for no\";
 cin>>ch;
 while((ch != \'y\') && (ch != \'n\'))
    {
 cout<<\"Please enter either y or n\";
 cin>>ch;
 }
 if(ch==\'y\')
 {
 cout<<\"Enter total expense for taxi\";
 cin>>taxi;
        total_charge=taxi-(20*days);
 }
 else
 total_charge=0;
    totalExpenses=totalExpenses+total_charge;
    return 20*days;
 }
 double registration()
 {
    double reg_fee;
    cout<<\" Enter registration fee \ \";
 cin>>reg_fee;
 while(reg_fee<0)
 {
 cout<<\" Enter correct registration fee\ \";
 cin>>reg_fee;
 }
    totalExpenses=totalExpenses+reg_fee;
 return 0;
 }
 double hotel(int days)
 {
    double rent;
    double total_charge;
    cout<<\"Enter hotel rent\";
    cin>>rent;
    totalExpenses=totalExpenses+rent;
    return days*90;
 }
 double meals(int days, double arrival, double departure)
 {
    int breakfast;
    int lunch;
    int dinner;
    if (departure<7.00)
        breakfast=days;
    else if(departure>7.00)
 breakfast=days-1;
 if(arrival>8.00)
 breakfast=breakfast;
 else if(arrival<8.00)
        breakfast=breakfast-1;
    if(departure<12.00)
        lunch=days;
    else if(departure>12.00)
        lunch=days-1;
    if(arrival>13.00)
        lunch=lunch;
    else if(arrival<13.00)
        lunch=lunch-1;
    if(departure<18.00)
        dinner=days;
    else if(departure>18.00)
        dinner=days-1;
    if(arrival>19.00)
        dinner=dinner;
    else if(arrival<19.00)
        dinner=dinner-1;
    double b,l,d;
    b=getBreakfast();
    l=getLunch();
    d= getDinner();
    totalExpenses=totalExpenses+(b*breakfast)+(l*lunch)+(d*dinner);
     return (9*breakfast)+(12*lunch)+(16*dinner);
}
 double getBreakfast()
 {
    double cost;
    cout<<\"Enter the cost of breakfast\ \";
    cin>>cost;
    return cost;
 }
 double getLunch()
 {
    double cost;
    cout<<\"Enter the cost of Lunch\ \";
    cin>>cost;
    return cost;
 }
 double getDinner()
 {
    double cost;
    cout<<\"Enter the cost of Dinner\ \";
    cin>>cost;
    return cost;
 }






