Search code examples
c++functionargumentsuser-inputparking

C++ Creating a parking charge


I am having to create a program that reads from the user what time they entered a parking garage and what time they left. The program uses this information to then figure out how much it costed the person to park there. For under 30 minutes, it's free. After 30 minutes and up to 2 hours, it is a $3 base charge, plus 5 cents every minute in excess of 30 minutes. Beyond 2 hours, it is an $8 base charge, plus 10 cents every minute in excess of 2 hours. I have so far got to convert the time the user inputted into all minutes. I am now stuck on what to do for the rest of my functions. I am new to programming, and the arguments in a function still confuse me. If you are able to help or provide any feedback, please tell in the comment section how the arguments were implemented to get the code to run correctly. Here is my code thus far:

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

I have updated my code with a working parking charge function. I am now aiming to get the print_results function working correctly and finding out how to get all of this to work together in the main function. Thanks to all for the help so far.


Solution

  • Your are almost done, You have need to call functions properly in main function. Declare two variables in main function totalTime and totalChargethen call the function

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