-5

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.

Trev.Drumm
  • 15
  • 2
  • 7
  • `cin>>entry_time` won't compile. You're missing the semicolon and `entry_time` is never declared. Please show us code that we can compile. – scohe001 Feb 07 '18 at 23:46
  • Okay, thanks for pointing that out. I added an 'int entry_time = 0;' and 'int exit_time = 0;' to the main function. I had a function that used entry_time and exit_time as arguments. I am confused when it comes to the whole function and argument things. – Trev.Drumm Feb 07 '18 at 23:48
  • 2
    Write and test one function at a time. Right now you've got a mess of syntax and logic errors that are going to play off one another and force you to do way more work than is necessary to untangle them all. Write a few lines, compile, fix errors. Repeat until you have a function done. Test the function. Do not proceed until you've proved that there are no errors in the function. Rush and maybe you save a bit of time, but it's more likely that waste time instead. – user4581301 Feb 07 '18 at 23:55
  • Okay, thanks. I've got a question that may help. Can I call a function I've already defined? Like how I've defined the "elapsed_time" function. I am wanting to use the information there inside "parking_charge". I did "int total = elapsed_time()", which is showing that it does not work. How would I go about that? – Trev.Drumm Feb 08 '18 at 00:01
  • @Trev.Drumm Yes you can call elapsed_time(). In parking_charge(), the second test should be "else if (total < 120)" – agbinfo Feb 08 '18 at 00:08
  • When I try to call it, I am getting an error saying "error: no matching function for call to to 'elapsed_time' – Trev.Drumm Feb 08 '18 at 00:11
  • elapased_time() takes 2 parameters. You provided none. – agbinfo Feb 08 '18 at 00:12
  • Like I said earlier, im very confused when it comes to the functions and its parameters. How do I know what two parameters to use when calling it? do I use entry and exit time again? – Trev.Drumm Feb 08 '18 at 00:15
  • In main(), you call print_results() with 2 arguments. You need to calculate these values before calling print_results. To do so, just before calling print_results, you can add 2 lines : "int total_minutes = elapsed_time(entry_time, exit_time);" and "double charge = parking_charge(total_minutes);" – agbinfo Feb 08 '18 at 00:19
  • Do you have a constraint that people cannot park there for more than a day? Otherwise your time keeping methodology is broken due to wrap around at 24h marker. `time_t` is guaranteed to be an arithmetic type and can keep track of any reasonable amount of time someone would park there. – Justin Randall Feb 08 '18 at 01:38
  • Justin, we are assuming that the person is only parking there for one day. Thanks for that tip though, it will be useful in future programs! – Trev.Drumm Feb 08 '18 at 03:46
  • @Agbinfo, I did what you said and now my only error is saying: error: invalid operands to binary expression ('ostream' (aka 'basic_ostream') and 'void')" Would you know what this means? – Trev.Drumm Feb 08 '18 at 03:55

1 Answers1

-2

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

    }
Sarmad Ali
  • 122
  • 1
  • 13
  • Hey, thanks for your help. It looks like your code is having far less errors than mine. But I am getting an error on line 44, which is the "cout<< print_results(charge). The compiler is telling me "error: invalid operands to binary expression ('ostream' (aka 'basic_ostream') and 'void')" – Trev.Drumm Feb 08 '18 at 03:30
  • The remaining error was happened because the function cout<< print_results(charge) is in the cout statement. Remove the cout statement and only call print_results() function. – Sarmad Ali Feb 08 '18 at 03:51
  • Thank you, this has fixed it and my program now runs smoothly!! – Trev.Drumm Feb 08 '18 at 04:07