Stuck on basic code

Page 1 of 1

3 Replies - 604 Views - Last Post: 28 September 2013 - 11:02 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=330147&amp;s=2f54f322fd3c6932d94552a854d76325&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Coderama

Reputation: 1
• Posts: 27
• Joined: 27-September 13

Stuck on basic code

Posted 27 September 2013 - 01:52 AM

Hi we got a homework assignment in which we have to print out a restaurant bill including the total, tax and tip. Problem is, the tip comes up as 0 in my code. I'm sure I'm making some silly mistake but I can't figure it out.
Here's my code:
```#include <iostream>

using namespace std;

int main()
{
float meal, tax, tip, total;
meal = 445;
tax = (6.75/100)*meal;
total = meal+tax;
tip = (15/100)*total;

cout<<"Total meal cost is " <<total <<endl;
cout<<"Tax is " <<tax <<endl;
cout<<"Tip is " <<tip;

```

Is This A Good Question/Topic? 0

Replies To: Stuck on basic code

• Saucy!

Reputation: 6207
• Posts: 23,953
• Joined: 23-August 08

Re: Stuck on basic code

Posted 27 September 2013 - 04:10 AM

http://c-faq.com/expr/truncation1.html

#3 Mrk

Reputation: 29
• Posts: 66
• Joined: 03-December 09

Re: Stuck on basic code

Posted 27 September 2013 - 11:39 PM

Every variable must have a type:

int x = 0;
float y = 0;
double z = 0;

Likewise, any literal value also has a type:

15 means an integer with a value of 15.
15.0 means a double with a value of 15.
15.0f means a float with a value of 15.

Any number that has a decimal point in it is automatically a double value, unless it is followed by an "f" in which case it is a float, and any number without a decimal point is an integer. Notice line 9 in your code:

tax = (6.75/100)*meal;

You have a double literal, 6.75 which results in a double result ((6.75/100) *meal) being assigned to "tax" which is a float. The compiler has to convert the double result into a float to fit it inside "tax", so you get this warning: "conversion from 'double' to 'float', possible loss of data". In this case there happens to be no loss of data so no problem. But it's good to eliminate all warnings anyway, so putting an "f" after 6.75 (6.75f) causes the result to be a float value which is assigned to the float "tax", and so no warning.

tip = (15/100)*total;

So you are dividing the integer 15 by the integer 100 and getting less than zero, a fractional part for a result. And you know an integer cannot hold fractional parts. So (15/100) = 0 when dealing with integers. So just change those two literals to floats like so:

tip = (15.0f/100.0f)*total; .... or you could just do this:
tip = (15.0f/100)*total; .....and that would work just as well. Or you could:
tip = (15.0/100)*total; ...... but you would get that warning about the double having to be converted into a float to fit within "tip" which isn't good practice (but in this case works just fine).

And just so you know (and maybe you already do : ) but you are using magic numbers in your code. It's not good practice to use "magic numbers", like what is (6.75/100) supposed to do? And why? Also ---> (15/100) what's this? What magic does that quantity perform? And how? It would be better to do it this way:

```#include <iostream>

using namespace std;

int main()
{
float meal, tax, tip, total;

float TaxRate = (6.75f/100);
float TipRate = (15.0f/100);

meal = 445;
tax = TaxRate * meal;      easier to understand.
total = meal+tax;
tip = TipRate * total;     easier to understand.

cout<<"Total meal cost is " <<total <<endl;
cout<<"Tax is " <<tax <<endl;
cout<<"Tip is " <<tip;
}

```

Another advantage to doing it this way is that if the tip rate or the tax rate should change later, and you used those "magic numbers" all over the place (if your program was much larger) then you would have to go throughout your whole program and track down each time you used those magic numbers to change them. But if you used variables instead, all you would have to do is update the variables "TaxRate" and "TipRate" once and the rest of your program would be all set, rather than looking throughout your whole program to find and update all of those magic numbers that you used all over the place!

It would also be good to make those variables constant, like so:

const float TaxRate = (6.75f/100);
const float TipRate = (15.0f/100);

This post has been edited by Mrk: 28 September 2013 - 12:03 AM

#4 Coderama

Reputation: 1
• Posts: 27
• Joined: 27-September 13

Re: Stuck on basic code

Posted 28 September 2013 - 11:02 PM

Mrk, on 27 September 2013 - 11:39 PM, said:

x

Thank you so much! That helps a lot!