C++ 函数风格的问题

Trouble with c++ function style

#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 之外的一个单独的函数中,但我不确定是否正确地执行此操作,因为我是这个函数的新手,而且我所拥有的书展示了如何构建这个函数的错误示例。

这是您要找的吗?

说'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 你应该得到...无论用户输入什么。

希望对您有所帮助!

以下实施仅针对“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();
}

希望对您有所帮助!!