麻烦c ++函数风格

时间:2015-02-11 07:31:39

标签: c++ function structure

#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之外的单独函数中,但我不确定这样做是否正确导致我对这个函数的新东西和我所拥有的书是如何显示如何的坏例子结构这个功能。

2 个答案:

答案 0 :(得分:1)

这是你正在寻找的吗?

说出&#39; getdays&#39;功能;它应该这样定义:

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

希望它有所帮助!

答案 1 :(得分: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();
}

希望它有所帮助!!