How to get rid of warning?

Feb 16, 2013 at 12:59am
I keep getting a [Warning] converting ti 'int' from 'double'

But...that's sort of the goal. Is this just something to ignore or am I doing something wrong?

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
#include <iostream>

using namespace std;

int main()
{
	double hours = 0.0;
        char parkingLevel = ' ';
	double fee = 0.0;
	double totalFee= 0.0;
	int roundHours= 0;
	
	cout << "Please enter the level on which you are parked:";
	cin >> parkingLevel;
	cout << "Please enter the number of hours you were parked:";
	cin >> hours;

	roundHours = hours + 0.5;

	if (parkingLevel == 'A' || parkingLevel == 'a')
	{
		totalFee = 7.50 + 3 * (roundHours - 2);
	}
	else if (parkingLevel == 'B' || parkingLevel == 'b')
	{
		fee = 7.50 + 3 * (roundHours - 2);
		totalFee = fee - (fee * .05);
	}
	else if (parkingLevel == 'C' || parkingLevel == 'c')
	{
		fee = 7.50 + 3 * (roundHours - 2);
		totalFee = fee - (fee * .10);
	}

	cout << "The total fee is: $" << totalFee << endl;

	system("pause");
	return 0; 
}


Also i've just discovered I'm having problems with one of my test cases...

TEST CASE 2:
Parked in level A for 2 hours and 10 minutes
First two hours pay 7.50
Next 10 minutes = $3.00
Total fee equals $10.50

When I enter 2. anything though it comes out as $7.5... I thought roundHours = hours + 0.5; would fix this...
Last edited on Feb 16, 2013 at 1:10am
Feb 16, 2013 at 1:10am
You can probably set your compiler to not warn you about this. Another way is to change your code and do the cast explicit.
roundHours = static_cast<int>(hours + 0.5);
Feb 16, 2013 at 1:13am
Thank you! Changing the code got rid of the warning, but I'm still having problems with 2.whatever not converting properly...unless there's something else wrong I'm just not seeing.
Feb 16, 2013 at 1:18am
I don't see how your output could be produced by the code you posted.
Feb 16, 2013 at 1:24am
I'm sorry if I'm not being clear... the problem parameters state

First two hours = $7.50
All succeeding hours (and fractions thereof) the fee is $3.00 per hour

Therefore when I enter 2.1, I should be able to get 10.50 as my result

But I'm not getting that answer...for only that test case... am i doing something wrong here?

Feb 16, 2013 at 1:49am
Is there something wrong with my function? Or is my conversion not working?
Feb 16, 2013 at 2:13am
2.1 hours is not 2 hours and 10 minutes. 2.1 hours is 2 hours and a tenth of an hour. You may run into problems with your addition of 0.5 to get the roundHours. For example if the user input 2.8 hours (which is acceptable as 2 hours 48 minutes) the addition of 0.5 will take it to 3.3 roundHours which will charge for an extra hour (extra $3) when the fee should only be 2 hours @ $7.50 + 1hr @ $3 (the same as the 2.1 mentioned earlier).

Edit: Look into whether integers round to the nearest whole number. I think they always round up, or down.


What cire said.... :p
Last edited on Feb 16, 2013 at 2:20am
Feb 16, 2013 at 2:16am
If hours is 2.1, then:

roundHours = hours + 0.5; will be 2.

If hours is 2.4, then roundHours will be 2.

When converting from int to double, the fractional portion is dropped.

It looks like you want to do:

1
2
3
4
#include <cmath>
    // ...
    roundHours = static_cast<int>(std::ceil(hours)) ;
    // ... 
Feb 16, 2013 at 2:37am
Ohhhh wow that works...thank you so much!

My professor has not introduced anything pertaining to <cmath> though? Is there any possible way to perform this function using the method I was using, or is the ceil function the only way?


Ps.... I'm very sorry if I'm being frustrating I'm very new to this, thank you so much for any and all help you see fit to provide
Feb 16, 2013 at 3:04am
You could do something like:

1
2
3
4
    roundHours = static_cast<int>(hours) ;

    if ( hours > roundHours )
        ++roundHours ;

Feb 16, 2013 at 3:25am
Ohh I see! Thank you again for all your help!!!
Topic archived. No new replies allowed.