开发者

C++ For Loop Problem?

开发者 https://www.devze.com 2023-02-10 12:05 出处:网络
Ok so i\'m working on this project and its a travel expense program. Basically it just has some functions that gets info from the user. I\'m having a problem with my for loop. The running total is mes

Ok so i'm working on this project and its a travel expense program. Basically it just has some functions that gets info from the user. I'm having a problem with my for loop. The running total is messing up. The numbers end up like 2 or 4 numbers off of what there supposed to be. Here's the code(I know it's not neat or anything i will clean that up later)

#include <iostream>
#include <fstream>
using namespace std;


int getDays(int);
double getDepartureTime();
double getArrivalTime(double);
double airFees(double);
double carRentalFees(double);
double getMilesDriven(double);
double getParkingTotal(double,double);
double getParkingSpent(double,double);
double getTaxiFees(double,double);
double employeeHotelExpense(double,double);
double getHotelExpense(double,double);
double getMealExpenses(double,double);
void timeEquivalent();
double breakFastFee = 0;
int main()
{
    int days=0, amount=0, departure_conference=0, departure_home=0,time = 0;
    double airFee=0,taxiFeesAllowed,parkingAllowed = 0,employeeHotelExpense = 0,employeeTaxiFees = 0, milesDriven=0, 
    parkingFees=0, taxiFees=0, yes=0,arrivalTime = 0;
    double carRentalFee = 0, hotel_expenses=0,departureTime = 0, meals=0,employeeMealExpenses = 0, parkingSpent = 0,allowableHotelExpense = 0
    ,allowedMealTotal = 0,mealsSpent = 0;
    char employee[40];
    //timeEquivalent();
    //cout << "What Time Did You Arrive " <<endl;
    //cin >>time;

    days = getDays(days);
    timeEquivalent();
    departureTime = getDepartureTime();
    arrivalTime = getArrivalTime(arrivalTime);
    airFee = airFees(airFee);
    carRentalFee = carRentalFees(carRentalFee);
    milesDriven = getMilesDriven(milesDriven);
    parkingAllowed = 6 * days;
    parkingSpent = getParkingSpent(parkingSpent,days);
    taxiFeesAllowed = days * 10;
    employeeTaxiFees = getTaxiFees(taxiFees,days);
    allowableHotelExpense = 90 * days;
    employeeHotelExpense = getHotelExpense(employeeHotelExpense, days);
    employeeMealExpenses = getMealExpenses(departureTime,arrivalTime);
    cout << employeeMealExpenses <<endl;


    return 0;



}
    int getDays(int days)
    {
        cout << " How many Days did you stay on the trip " <<endl;
        cin >> days;

        while(days < 0)
        {
            cout <<"Please enter a value greater than 0 :D " <<endl;
            cin >> days;
        }


        return days;
    }

    double getDepartureTime()
    {
        double departureTime;

        cout << "Please Refer To The Menu Above and enter the time of departure in military\n";
        cout << "Time. For example if you departed at 7:30 enter 0730\n\n";

        cin >> departureTime;
        return departureTime;
    }

    double getArrivalTime(double arrivalTime)
    {
        cout << "Please refer to the menu above and enter the time you arrived back home in\n";
        cout <<" military format\n";
        cin >> arrivalTime;
        return arrivalTime;
    }
    double airFees(double airfee)
    {
        cout << " How Much Were Your Air Fees " <<endl;
        cin >> airfee;
        while(airfee < 0)
        {
            cout <<" Please enter a value greater than 0 :D " <<endl;
            cin >> airfee;
        }


        return airfee;
    }

    double carRentalFees(double carRentalFee)
    {
        cout << " How Much were Your Car Rental Fees " <<endl;
        cin >> carRentalFee;
        while(carRentalFee < 0)
        {
            cout <<"Please enter a value of 0 or greater :D " <<endl;
            cin >> carRentalFee;
        }


        return carRentalFee;
    }
    double getMilesDriven(double milesDriven)
    {
        const double mileRate = 0.27;
        cout << " How many miles did you drive, please enter 0 if a private vehicle was not used " <<endl;
        cin >> milesDriven;
        while(milesDriven < 0)
        {
        cout << " Please Enter 0 or Greater:)"<<endl;
        cin >> milesDriven;
        }
        return mileRate * milesDriven;
    }

    double getParkingSpent(double parkingSpent, double days)
    {

        cout << " How Much Did You Spend on Parking " <<endl;
        cin >> parkingSpent;
        while(parkingSpent < 0)
        {
            cout << "Please Enter an Amount of 0 or Greater "<<endl;
            cin >> parkingSpent;
        }
        return parkingSpent*days;
    }
    double getTaxiFees(double taxiFees,double days)
    {
        cout << " Please Enter The Amount of Taxi Fees Please " <<endl;
        cin >> taxiFees;
        while(taxiFees < 0)
        {
            cout << "Please Enter an Amount of 0 or Greater "<<endl;
            cin >> taxiFees;
        }
        return taxiFees * days;

    }

    double getHotelExpense(double employeeHotelExpense,double days)
    {
        cout << " How Much Were Your Hotel Expenses " <<endl;
        cin >> employeeHotelExpense;
        while(employeeHotelExpense < 0)
        {
            cout << "Please Enter a Amount of 0 or Greater "<<endl;
            cin >> employeeHotelExpense;
        }
        return employeeHotelExpense * days;
    }
    double getMealExpenses(double departureTime,double arrivalTime)
    {
        static double breakFastFee = 0 ;
         static double lunchFee = 0 ;
        static double dinnerFee = 0 ;
        int numberOfDays = 2 ;
        double total = 0;

        for(int days =1;days <=numberOfDays;days++)
        {

            if ( days < numberOfDays && departureTime > 000 && departureTime < 700)
            {
                cout << "Please Enter Your breakfast cost"<<endl;
                cin >> breakFastFee;


                cout << " Please Enter Your Lunch Cost " <<endl;
                cin >>lunchFee;

                cout << "Please Enter Your Dinner Cost " <<endl;
                cin >> dinnerFee;
            }

         if (days < numberOfDays && departureTime > 700 && departureTime <=1200)
            {
                cout << "Please Enter Your Lunch Cost"<<endl;
                cin >> lunchFee;

                cout << "Please Enter Your Dinner cost "<<endl;
                cin >> dinnerFee;

        }
            if(days < numberOfDays && departureTime > 1201 && departureTime <= 1800)
            {
                cout << "Enter The Cost of Dinner " <<endl;
                cin >> dinnerFee;
            }

            if(days == numberOfDays && arrivalTime > 800 && arrivalTime<=1300)
            {
                cout <<"Enter The Cost of Breakfast " <<endl;
                cin >> breakFastFee;
            }

            if(days == numberOfDays && arrivalTime > 1301 && arrivalTime <= 1900)
            {
                cout << "Enter The Cost of Breakfast "<<endl;
                cin >> breakFastFee;

                cout << " Enter The Cost of Lunch " <<endl;
                cin >> lunchFee;
            }

            if(days == numberOfDays && arrivalTime > 1901)
            {
                cout << "Enter The Cost of Breakfast " <<endl;
                cin >> breakFastFee;

                cout << " Enter The Cost of Lunch " <<endl;
                cin >> lunchFee;

                cout << "Enter The Cost of Dinner " <<endl;
                cin >> dinnerFee;
    }

        total+=breakFastFee + lunchFee + dinnerFee;


    }

        return total;
    }



    void timeEquivalent()
    {

        cout <<"Regular Time " << "\t\t" <<"Military Time \n";
        cout <<"************"  << "\t\t" <&开发者_运维技巧lt;"***************"<<endl;
        cout <<"Midnight "     << "\t\t" <<"0000 \n";
        cout <<"1:00a.m. "     << "\t\t" <<"0100 \n";
        cout <<"2:00a.m. "     << "\t\t" <<"0200 \n";
        cout <<"3:00a.m. "     << "\t\t" <<"0300 \n";
        cout <<"4:00a.m. "     << "\t\t" <<"0400 \n";
        cout <<"5:00a.m. "     << "\t\t" <<"0500 \n";
        cout <<"6:00a.m. "     << "\t\t" <<"0600 \n";
        cout <<"7:00a.m. "     << "\t\t" <<"0700 \n";
        cout <<"8:00a.m. "     << "\t\t" <<"0800 \n";
        cout <<"9:00a.m. "     << "\t\t" <<"0900 \n";
        cout <<"10:00a.m."     << "\t\t" <<"1000 \n";
        cout <<"11:00a.m."     << "\t\t" <<"1100 \n";
        cout <<"12:00p.m."     << "\t\t" <<"1200 \n";
        cout <<"1:00p.m. "     << "\t\t" <<"1300 \n";
        cout <<"2:00p.m. "     << "\t\t" <<"1400 \n";
        cout <<"3:00p.m. "     << "\t\t" <<"1500 \n";
        cout <<"4:00p.m. "     << "\t\t" <<"1600 \n";
        cout <<"5:00p.m. "     << "\t\t" <<"1700 \n";
        cout <<"6:00p.m. "     << "\t\t" <<"1800 \n";
        cout <<"7:00p.m. "     << "\t\t" <<"1900 \n";
        cout <<"8:00p.m. "     << "\t\t" <<"2000 \n";
        cout <<"9:00p.m. "     << "\t\t" <<"2100 \n";
        cout <<"10:00p.m."     << "\t\t" <<"2200 \n";
        cout <<"11:00p.m."     << "\t\t" <<"2300 \n";
        cout <<"Midnight "     << "\t\t" <<"0000 \n";


    }

can somebody tell me whats going wrong. If you test it out use 0600 for departure time and 0900 for arrivaltime.


There are multiple problems in this code. One, in a function like getDays that returns the number of days, you don't need to pass the number of days into the method as a parameter.

Also, since you want a value for days that is greater than 0, you should be checking whether days <= 0 in the while loop. Your current condition, days < 0, will be false if days is set to 0.

getDays would be better written as:

int getDays()
{
    int days = 0;

    cout << " How many Days did you stay on the trip " <<endl;
    cin >> days;

    while(days <= 0)
    {
        cout <<"Please enter a value greater than 0 :D " <<endl;
        cin >> days;
    }

    return days;
}

Also, in getMealExpenses, there seems to be no reason to declare breakFastFee, lunchFee, and dinnerFee as static. This may be the cause of the problem you asked about, since they never get re-initialized to 0 after the first call to getMealExpenses.

Finally, neatly-formatted code is more likely to get helpful responses, because it's easier to read :-)


I think its becouse you are not resetting the breakFastFee, lunchFee, dinnerFee variable to 0 before each execution. So when total+=breakFastFee + lunchFee + dinnerFee; hits it will add values from the previues iteration.

0

精彩评论

暂无评论...
验证码 换一张
取 消