2015-02-11 42 views
-2
#include <iostream> 
#include <iomanip> 
using namespace std; 

//Function prototypes 
int getDays(); 
double getDepartureTime(); 
double getArrivalTime(); 
double getAirfareFees(); 
double getRentalFees(); 
int getMileageFees(); 
double getParkingFees(int days); 
double getTaxiFees(int days); 
double getRegistrationFees(); 
double getHotelExpenses(int days); 
double getBreakfastExpenses(int days, double dTime, double aTime); 
double getLunchExpenses(int days, double dTime, double aTime); 
double getDinnerExpenses(int days, double dTime, double aTime); 

//Global constants 
const double MILES = 0.27; //Expense per miles driven 
const int PARKING = 6; //Allowed daily parking allowance 
const int TAXI = 10; //Allowed daily taxi allowance 
const int HOTEL = 90; //Allowed nightly hotel allowance 
const int BKFST = 9; //Allowed daily breakfast allowance 
const int LUNCH = 12; //Allowed daily lunch allowance 
const int DINNER = 16; // ALlowed daily dinner allowance 

int main() 
{ 
//Variable Declaration 
    double grandTotal = 0, //Total expenses incurred 
     allowedTotal = 0, //Total allowable expenses for the trip 
     reimburseTotal = 0, //Excess that must be reimbursed, if any 
     savedTotal = 0, //Amount saved, if any 
     mealExpenses; //Total cost incurred for meals 

//Input & function processing 

//Bullet 1 
    int days = getDays(); 
{ 
    int getDays(); 
} 
{ 
    int days; 
    cout << "Enter total of days you'll be staying: "; 
    cin >> days; 
    if (days < 0) 
    cout << "Days cannot be less than 0 \n\n" 
     << "Enter total of days you'll be staying again: \n"; 
    cin >> days; 
} 

//Bullet 2 
    double dTime = getDepartureTime(); 
{ 
    double getDepartureTime(); 
} 
{ 
    cout << "Enter your departure time: "; 
    cin >> dTime; 
    if (dTime < 0) 
{ 
    cout << "Time cannot be less than 0 \n" 
     << "Enter departure time again: \n"; 
    cin >> dTime; 
} 
    else if (dTime > 24) 
{ 
    cout << "Time cannot exceed more than 24 hours \n" 
     << "Enter departure time again: \n"; 
    cin >> dTime; 
} 
} 

這段代碼將被分成單獨的函數,從它的外觀我可能會搞亂。我所擁有的代碼應該在int main之外的獨立函數中,但是我不確定是否正確執行此操作會導致我對這個函數有所瞭解,而且本書中顯示瞭如何構造此函數的錯誤示例。C++函數風格的問題

+0

你的問題是什麼?你的代碼很奇怪。你可能想再次閱讀一些基礎知識。 – 2015-02-11 07:40:08

+0

http://www.cplusplus.com/doc/tutorial/functions/。 – rubikonx9 2015-02-11 07:40:44

+0

您嘗試閱讀有關類和成員函數的內容,通過您的代碼的外觀,將類中的類似函數分組將會更好地滿足您的需求。例如,爲費用,費用,時間,津貼創建班級,並創建獲取和設置功能。它與其他4人組成的團隊項目是 – Sridhar 2015-02-11 07:57:33

回答

0

以下實現僅針對「int getDays()」。你必須以這種方式修改所有這些方法。

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

//Function prototypes 
struct HotelMenu 
{ 
    HotelMenu() 
    { 
     MILES = 0.27; //Expense per miles driven 
     PARKING = 6; //Allowed daily parking allowance 
     TAXI = 10; //Allowed daily taxi allowance 
     HOTEL = 90; //Allowed nightly hotel allowance 
     BKFST = 9; //Allowed daily breakfast allowance 
     LUNCH = 12; //Allowed daily lunch allowance 
     DINNER = 16; // ALlowed daily dinner allowance 
    } 
    int getDays(); 
    double getDepartureTime(); 
    double getArrivalTime(); 
    double getAirfareFees(); 
    double getRentalFees(); 
    int getMileageFees(); 
    double getParkingFees(int days); 
    double getTaxiFees(int days); 
    double getRegistrationFees(); 
    double getHotelExpenses(int days); 
    double getBreakfastExpenses(int days, double dTime, double aTime); 
    double getLunchExpenses(int days, double dTime, double aTime); 
    double getDinnerExpenses(int days, double dTime, double aTime); 

    private: 
    //Global constants 
    const double MILES; 
    const int PARKING; 
    const int TAXI; 
    const int HOTEL; 
    const int BKFST; 
    const int LUNCH; 
    const int DINNER; 
} 

int HotelMenu::getDays() 
{ 
    int days; 
    cout << "Enter total of days you'll be staying: "; 
    cin >> days; 
    if (days < 0) 
    cout << "Days cannot be less than 0 \n\n" 
     << "Enter total of days you'll be staying again: \n"; 
    cin >> days; 

    return days; 
} 

double HotelMenu::getDepartureTime() 
{ 
    double dTime; 
    cout << "Enter your departure time: "; 
    cin >> dTime; 
    if (dTime < 0) 
    { 
     cout << "Time cannot be less than 0 \n" 
      << "Enter departure time again: \n"; 
     cin >> dTime; 
    } 
     else if (dTime > 24) 
    { 
     cout << "Time cannot exceed more than 24 hours \n" 
      << "Enter departure time again: \n"; 
     cin >> dTime; 
    } 
    return dTime; 
} 

int main() 
{ 
    //Variable Declaration 
    double grandTotal = 0, //Total expenses incurred 
     allowedTotal = 0, //Total allowable expenses for the trip 
     reimburseTotal = 0, //Excess that must be reimbursed, if any 
     savedTotal = 0, //Amount saved, if any 
     mealExpenses; //Total cost incurred for meals 

    // Create an instance of the structure 
    HotelMenu aInstance; 

    //Input & function processing 

    //Bullet 1 
    int days = aInstance.getDays(); 

    //Bullet 2 
    double dTime = aInstance.getDepartureTime(); 
} 

希望它有幫助!

1

這是你在找什麼?

說出'getdays'功能;應該這樣定義的:

int getDays(){ 
    int days; 
    cout << "Enter total of days you'll be staying: "; 
    cin >> days; 
    if (days < 0)cout << "Days cannot be less than 0 \n\n"<< "Enter total of days you'll be staying again: \n"; 
    cin >> days; 
    return days; 
} 

int main你可以有int days=getDays()。 所以,當你cout << days你應該得到......無論用戶輸入。

希望它有幫助!