Please, please please someone help me. This might sound a little pathetic but I cannot figure this out for the life of me, it compiles just fine but i get the complete wrong output. Here is all the code I have.

The problem:

Write an application a that calculate the parking fee for a car for one day. The parking garage changes a $2.00 minimum fee for up to 3 hours. The garage charges an additional $0.50 per hour for each hour or partial hour in excess of three hours. The maximum charge is $10.00 for a 24 hour period. Assume that no car parks for longer than 24 hours at a time. Your application should ask for a total time parked in a decimal format and the state the total parking charge.

I try to enter 4.5 hours and get a total of 2.5$ when the answer should be 3$

The code:

#include<iostream>
using namespace std;

int main()
{
	float floatHour = 0;
	float floatFees = 0;
	int carryOver = 0, intHour = 0;

	cout<<"Enter number of hours parked: ";
	cin>>floatHour;

	if (floatHour <= 3)
		floatFees = floatFees  + 2.00;
	if (floatHour > 3)
		carryOver = (floatHour * 100);
		carryOver = carryOver % 100;
		intHour = ((floatHour * 100) / 100);
	if (carryOver == 0)
		floatFees = (intHour * .50) + floatFees;
	else if (carryOver > 0)
		floatFees = (intHour * .50) + floatFees + .50;
	 if (floatFees >= 10)
		floatFees = 10;


	cout<<"Your total is: $"<<floatFees<<endl;	


	system("PAUSE");

	return 0;
}

I got a little confused trying to follow the logic of the code.

floatHour equals 4.5, so line 15 is true, so we'll execute lines 16 to 18.

carryOver is 4.5 times 100 = 450.
Mod carryOver by 100 and we get 450 % 100 = 50.
intHour = 4.5 times 100 = 450, then divide by 100 and we get 4.5, but it's an integer, so we truncate. intHour is 4. Multiplying then dividing by 100 cancels itself out here. intHour = 4. Sems like that's what you want. carryOver is 50.

Since carryOver is 50, we'll skip lines 19 and 20 and execute lines 21 and 22.

floatFees is still 0 at the start of line 22, so we'll assign it to equal...

(4 times 0.50) + 0 + 0.50 = $2.50. Just what the computer said it was.


That's the process the computer went through. What was it SUPPOSED to do?

You needed to think this through a little bit more before trying to put all the situations of the program in together.

You need to make the program based off the words used in the assignment text. When it says minimum payment of $2 you should start the fee at $2 and then it says additional hours after are $0.50 each so you would figure out how many additional hours they stayed and add $0.50 for each hour/partial hour.

This is the "fixed up" version of your code

#include<iostream>
using namespace std;

int main()
{
	float floatHour = 0;
	float floatFees = 0;
	int carryOver = 0, intHour = 0;

	cout<<"Enter number of hours parked: ";
	cin>>floatHour;

	//if (floatHour <= 3) //this goes because the minimum is $2 no matter what
	floatFees += 2.00; //you can use the += operator for things like this

	if (floatHour > 3) //if you want all of these lines to be part of the if() you need to put them in braces (this isnt python because indentation doesnt matter)
	{
		floatHour -= 3; //subtract the three hours to get the amount of extra time they parked for
		carryOver = floatHour  * 100;
		carryOver = carryOver % 100;
		intHour = floatHour; //there is no point in multiplying by 100 then dividing by 100 as VernonDozier said

		//the only time this applies when the time is more than 3 hours so no point in doing pointless checking
		if (carryOver == 0)
			floatFees = (intHour * .50) + floatFees;
		else if (carryOver > 0)
			floatFees = (intHour * .50) + floatFees + .50;

		if (floatFees >= 10)
			floatFees = 10;
	}


	cout<<"Your total is: $"<<floatFees<<endl;

	return 0;
}

And here is a short way of doing this

#include <iostream>
using namespace std;

int main()
{
	float floatHour = 0;
	float floatFees = 2.00;

	cout << "Enter number of hours parked: ";
	cin >> floatHour;

	if (floatHour > 3)
	{
		floatFees += (int)(floatHour-3.00)*0.50;
		if( floatHour/(int)floatHour > 1 )
			floatFees += 0.50;

		if (floatFees > 10)
			floatFees = 10;
	}

	cout << "Your total is: $" << floatFees << endl;

	return 0;
}

Wow thank you both so much for you help, please allow me to apologies for my frustration and well belligerence ha. It wasn't a great day by any means, thank you again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.