C++ 创建停车费

C++ Creating a parking charge

我必须创建一个程序,从用户那里读取他们进入停车场的时间以及他们离开的时间。该程序使用此信息来计算该人将车停在那里的费用。不到 30 分钟,它是免费的。 30 分钟后至 2 小时内,基本费用为 3 美元,超过 30 分钟后每分钟加收 5 美分。超过 2 小时,基本费用为 8 美元,超过 2 小时后每分钟加收 10 美分。到目前为止,我必须将用户输入的时间转换为所有分钟。我现在陷入了如何处理其余功能的问题。我是编程新手,函数中的参数仍然让我感到困惑。如果您能够提供帮助或提供任何反馈,请在评论部分告诉我们如何实现参数以使代码正确地 运行。到目前为止,这是我的代码:

#include <iostream>
using namespace std;

int elapsed_time(int entry_time, int exit_time)
{
    int total = 0;
    total = (exit_time / 100) * 60 + (exit_time % 100) - (entry_time / 100) * 60
            + (entry_time % 100);
    return total;
} // returns elapsed time in total minutes

double parking_charge(int total_minutes) {
    double total = 0;
    double cents = 0;

if(total_mins < 30){
    return 0;
}
else if (total_mins <= 120){
    cents = (total_mins - 30) * 0.05;
    return total = 3 + cents;
}
else{
    cents = (total_mins - 120) * 0.10;
    return total = 4.5 + 8 + cents;
}
} // returns parking charge    

void print_results(total_minutes, double charge)
{
    cout << "The charge of parking was: " << parking_charge(total_minutes)
}

int main() {
int entry_time = 0;
int exit_time = 0;

    cout << "What was the entry time? (Enter in 24 hour time with no colon) ";
    cin >> entry_time;

    cout << "What was the exit time? (Enter in 24 hour time with no colon) ";
    cin >> exit_time;

    cout << print_results(total_minutes, charge);
}

我已经更新了我的代码,其中包含一个有效的停车收费功能。我现在的目标是让 print_results 函数正常工作,并找出如何让所有这些在 main 函数中一起工作。感谢大家迄今为止的帮助。

你快完成了,你需要在主函数中正确调用函数。在main函数中声明两个变量totalTimetotalCharge然后调用函数

    #include <iostream>
    using namespace std;

    int elapsed_time(int entry_time, int exit_time)
    {
    int total = 0;
    total = (exit_time / 100) * 60 + (exit_time % 100) - (entry_time / 100) * 60
            + (entry_time % 100);
    return total;
    } // returns elapsed time in total minutes

    double parking_charge(int total_minutes)
    {
      int charge;

      if (total_minutes <= 30)
        charge = 0;

      else if (120 > total_minutes < 30)
        charge = (3 + (0.05 * total_minutes));

      else
        charge = (8 + (0.10 * total_minutes));

    } // returns parking charge
    void print_results(double charge)
    {
       cout << "The charge of parking was: " << charge;
    }

    int main()
    {  
      double charge,minutes;
        int entry_time,exit_time;   
      cout << "What was the entry time? (Enter in 24 hour time with no colon) ";
      cin >> entry_time;

      cout << "What was the exit time? (Enter in 24 hour time with no colon) ";
      cin >> exit_time;

     minutes=elapsed_time(entry_time,exit_time);

     charge=parking_charge(minutes);  

     print_results( charge);

    }