# [Help very much appreciated] with time functions and calculations

• November 12th, 2012, 10:22 AM
AwkwardRaven
[Help very much appreciated] with time functions and calculations
I'm writing a program to calculate a fee based on the time that a car is parked in a theoretical garage. But my time function doesn't seem to be giving me the correct number of minutes... I would like to use the difftime function but I'm not sure exactly how to input my integers into the correct syntax in order to utilize said function.

Here's what I have so far:
Code:

```//Created by John Pearce // //Version 1.54 #include <iostream> #include <string> #include <iomanip> #include <cmath> #include <cstring> using namespace std; int feeTime = 0; float fee = 0; int enteringMins = 0, exitingMins = 0; int enterY, enterM, enterD, enterH, enterMi, exitY, exitM, exitD, exitH, exitMi; string entering, exiting; void convertStringToInt(); void calculateParkingHours(); float calculateFee(int); int main () {         cout << "Welcome to John's Parking Garage fee calculator." << endl;         cout << "fees are as follows: \$2.00 minimum up to three hours." << '\n'                 << "An additional \$0.50 per hour." << '\n'                 << "\$8.00 Flat fee for every 24 hours." << endl;         cout << setw(70) << setfill('*') << '\n' << endl;                 cout << "Please enter the date and time the car is entering the parking garage" << '\n'                         <<  "in the following format: MM/DD/YY hh:mm" << endl;                 getline(cin, entering);                 cout << '\n' << "Please enter the date and time the car is exiting the parking garage" << '\n'                         << "in the following format: MM/DD/YY hh:mm" << endl;                 getline (cin, exiting);                                 convertStringToInt();                 calculateParkingHours ();                 calculateFee(feeTime);                                 cout << '\n' << "Number of min parked: " << feeTime << ".  Fee: \$" << setprecision(2) << fixed << fee << endl; return 0; } void convertStringToInt() {         enterY = atoi ( entering.substr( 6, 2 ).c_str() );         enterM = atoi ( entering.substr( 0, 2 ).c_str() );         enterD = atoi ( entering.substr( 3, 2 ).c_str() );         enterH = atoi ( entering.substr( 9, 2 ).c_str() );         enterMi = atoi ( entering.substr( 12, 2 ).c_str() );                 exitY = atoi ( exiting.substr( 6, 2 ).c_str() );         exitM = atoi ( exiting.substr( 0, 2 ).c_str() );         exitD = atoi ( exiting.substr( 3, 2 ).c_str() );         exitH = atoi ( exiting.substr( 9, 2 ).c_str() );         exitMi = atoi ( exiting.substr( 12, 2 ).c_str() ); } void calculateParkingHours () { enteringMins += ( enterY * 365 * 29 * 60 ); enteringMins += ( enterM * 12 * 29 * 60 ); enteringMins += ( enterD * 29 * 60 ); enteringMins += ( enterH * 60 ); enteringMins += ( enterMi ); exitingMins += ( exitY * 365 * 29 * 60 ); exitingMins += ( exitM * 12 * 29 * 60 ); exitingMins += ( exitD * 29 * 60 ); exitingMins += ( exitH * 60 ); exitingMins += ( exitMi ); feeTime = ((enteringMins - exitingMins) * -1); } float calculateFee(int feeTime) {         if (feeTime >= 1440)         {                 fee = 8.00;                 if (feeTime >= 2880)                 {                         fee += ((feeTime - 1440) / 1440) * 8.00;                         if ((feeTime - 1440) % 1440)                         fee += 8.00;                 }                }         else if (feeTime >= 180)         {                 fee = 2.00;                 if (feeTime > 180)                 {                         fee += ((feeTime - 180) / 60) * 0.5;                         if ((feeTime - 180) % 60)                         fee += .50;                 }                 if (fee > 10)                         fee = 10;         }         else if (feeTime < 180)         {                 fee = 2.00;         }         return fee; }```
• November 12th, 2012, 10:53 AM
GCDEF
Re: [Help very much appreciated] with time functions and calculations
The links to documentation in your other thread showed you everything you need to know. Start with populating the tm struct, then using mktime to create the time_t variables that difftime requires. Also, stick to one thread for the same question.