Ok so I have this program that takes a set of times and dates (in the format of yy/mm/dd hh:mm) and creates a 'parking fee'. Only problem is some numbers are off.
// This program takes in two sets of date and time to give a 'fee' for the total amount of hours.
#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>
usingnamespace std;
int parseDate(string); //Prototype for my string-to-integer and dates-to-minutes subprogram
float totalCharge(float); //Prototype
int main ()
{
string enter;
int startDateMins;
int endDateMins;
int elapsedTime;
float totalFee;
cout << "Please enter the date and time the car is entering "<< endl
<< "the parking garage in the following format: YY/MM/DD hh:mm"<< endl;
getline (cin, enter);//cin >> enter_date >> enter_time;
startDateMins = parseDate(enter);
cout<< "Please enter the date and time the car is exiting "<< endl
<< "the parking garage in the following format: YY/MM/DD hh:mm"<< endl;
getline (cin, enter);//cin >> exit_date >> exit_time;
endDateMins = parseDate(enter); //Program call
elapsedTime = endDateMins - startDateMins; //Program call
totalFee = totalCharge(elapsedTime); //Program call
cout << fixed << showpoint << setprecision(2) << "Your fee is " << "$" << totalFee << endl;
return 0;
}
int parseDate(string dateStr)
{
int year = atoi( dateStr.substr( 0, 2 ).c_str() );//
int month = atoi( dateStr.substr( 3, 2 ).c_str() );//
int day = atoi( dateStr.substr( 6, 2 ).c_str() );// These convert string values into integers
int hour = atoi( dateStr.substr( 9, 2 ).c_str() );//
int min = atoi( dateStr.substr( 12, 2 ).c_str() );//
int totalMins = 0; //
totalMins += ( year * 365 * 24 * 60 ); //
totalMins += ( month * 30 * 24 * 60 ); // This calculates the total amount od minutes in the time frame
totalMins += ( day * 24 * 60 ); //
totalMins += ( hour * 60 ); //
totalMins += ( min ); //
return totalMins;
}
float totalCharge(float time)
{
int hours;
int days;
float fee;
float extrahour;
hours = time/60.0; //Calculate the number of hours that fit into the given time
if (time >= 1440) // Calculate the total number of days from the total amount of minutes
days = time/1440.0;
elseif (time > 1440)
{
extrahour = time-1440.0; // Calculate extra hours past a day
extrahour = extrahour/60;
}
if (hours > 0 && hours <= 3) // $2 parking fee good for up to 3 hours
fee = 2;
elseif (hours > 3 && hours <= 24) //50c fee per extra hour after 3 hours
fee = 2 + .50*(hours-3);
elseif (hours > 24 && hours < 48 ) //$10 max daily fee
fee = 10 + .50*extrahour;
elseif (hours >= 48 ) // If longer than 24 hours, then $8 fee/day
fee = 8*days + .50*extrahour;
return (fee);
}
So my results should be the following with these inputs, however some of my answers are short by 50 cents.
Gave that a shot and its still no good. For example, the test that should be giving me 3:50 is giving me 3:00, and the test that should give me $10 is giving me 9:50. The only case that came out correct was the first one.
I think there's an issue with your code flow; line 70 will never trigger.
At 67, you check if time is "equal or over 1440". On line 70, the else begins, which means it only triggers in case of "under 1440". Since you check for over 1440, it will never work. I think removing the else on 70 will be sufficient, but I haven't really checked what you're doing.