C ++ For循环问题?

时间:2011-02-14 00:54:41

标签: c++ for-loop

好的,我正在研究这个项目及其旅行费用计划。基本上它只是有一些从用户获取信息的功能。我的for循环有问题。跑步总数搞乱了。这些数字最终会变成2或4个数字。这是代码(我知道它不整齐或我以后会清理它的任何东西)

#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" <<"***************"<<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";


    }

有人可以告诉我什么是错的。如果您测试出来,请使用0600作为出发时间,使用0900作为到达时间。

2 个答案:

答案 0 :(得分:3)

此代码中存在多个问题。一,在像getDays这样的函数返回 天数,你不需要传递 作为参数进入方法的天数。

此外,由于您希望days的值大于0,因此您应该检查while循环中是否days <= 0。如果天数设置为0,您当前的状况days < 0将为false

getDays最好写成:

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;
}

此外,在getMealExpenses中,似乎没有理由将breakFastFeelunchFeedinnerFee声明为static。这可能是您询问问题的原因,因为在第一次调用getMealExpenses后,它们永远不会重新初始化为0.

最后,整齐格式的代码更有可能获得有用的响应,因为它更容易阅读: - )

答案 1 :(得分:0)

我认为这是因为在每次执行之前你没有将breakFastFee, lunchFee, dinnerFee变量重置为0。因此,当total+=breakFastFee + lunchFee + dinnerFee;命中时,它将添加上一次迭代的值。