Proper int-to-float conversion

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// 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>

using namespace 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;

	else if (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;
	else if (hours > 3 && hours <= 24) //50c fee per extra hour after 3 hours
		fee = 2 + .50*(hours-3);
	else if (hours > 24 && hours < 48 ) //$10 max daily fee
		fee = 10 + .50*extrahour;
	else if (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.

12/06/12 05:28 12/06/12 07:15 $2.00
12/11/03 15:58 12/11/03 21:09 $3.50
12/04/15 21:08 12/04/16 04:27 $4.50
12/03/21 10:35 12/03/22 05:03 $10.00
12/10/18 13:02 12/10/19 12:46 $10.00
12/08/09 08:53 12/08/12 06:57 $24.00
12/05/13 16:18 12/05/20 18:35 $64.00

I also notice that any fee above $10 gave me a debug error. Ive tried tinkering with it to no avail.
fee is a float but hours is an int. The division 0.5*int is interpreted as integer division. You can cast it to float:

fee = 2 + 0.5*((float)hours-3);
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.
You'll have to do that with days as well.

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.
Topic archived. No new replies allowed.