# Calculating percentages

Page 1 of 1

## 2 Replies - 633 Views - Last Post: 11 February 2014 - 10:23 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=339838&amp;s=453b4bb79604a28fdf2da40318cd279a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 SuperSlow

Reputation: 0
• Posts: 16
• Joined: 16-October 12

# Calculating percentages

Posted 11 February 2014 - 09:03 PM

I am using codeblocks for school and I am trying to create a program to calculate a percentage for commission and a bonus. I have tried using a decimal for percentage, int and floats with casting. Every time it I try it displays 0 or just the bonus. I even took and did a test page. My head is about to pop.

Write an algorithm that can be used to calculate the commission earned in a real estate
transaction. The chart below describes the formulas used to calculate the commission.

Sales Price Commission
Less than \$100,000 5% of Sales Price
\$100,000 to \$300,000 \$5,000 + 10% of Sales Price over \$100,000
More than \$300,000 \$25,000 + 15% of Sales Price over \$300,000

```// Commission and Bonuses.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
using namespace std;

int main()
{

float salePrice,
commission,
pcntA = 5/100,
pcntB = 10/100,
pcntC = 15/100,
bonusB = 5000,
bonusC = 25000,
saleB,
saleC;

cout << "Commission earned in a real estate transaction.\n" << endl;
cout << "Less than \$100,000 get 5% of Sales Price." << endl;
cout << "\$100,000 to \$300,000 get \$5,000 + 10% of Sales Price." << endl;
cout << "More than \$300,000 get \$25,000 + 15% of Sales Price.\n" << endl;

cout << "Input Sales Price.\n\n" << endl;  // Input Feet First(That is funny).
cin >> salePrice;

if (salePrice < 100000) { // commission for less than 100,000
commission = salePrice * pcntA;
}
else if (salePrice > 300000) { // commission and bonus for greater than 300,000
saleC = salePrice * pcntC;
commission = saleC + bonusC;
}

else { // commission and bonus for range 100,000 to 300,000
saleB = salePrice * pcntB;
commission = saleB + bonusB;
}

cout << commission << " is you Commission for this sale.\n\n" << endl;  // Display Meters.

system ("PAUSE");  // Without the added pause the screen would not give any time to display the answer.

return 0;  // End of program.
}

```

```#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
using namespace std;

int main()
{

float salePrice,
commission,
saleB,
saleC,
bonusB = 5000,
bonusC = 25000,
pcntA = 5/100,
pcntB = 10/100,
pcntC = 15/100;

float percentage;
percentage = (10/90000) * 100;

cout << percentage;

system ("PAUSE");  // Without the added pause the screen would not give any time to display the answer.

return 0;  // End of program.
}

```

I will even sacrifice a goat to the dreamincode gods if that is what it takes.

Is This A Good Question/Topic? 0

## Replies To: Calculating percentages

### #2 jimblumberg

Reputation: 4657
• Posts: 14,599
• Joined: 25-December 09

## Re: Calculating percentages

Posted 11 February 2014 - 09:26 PM

First look at this snippet:
```        pcntA = 5/100,
```

Even though pcntA is a floating point number the calculation is an integer calculation, meaning there are no fractions and the above snippet evaluates to zero. To force floating point math you will need to have at least one floating point number in the calculation, I recommend you use all floating point numbers. When dealing with constants you need to include the decimal point to force floating point math.

```        pcntA = 5.0/100.0,
```

Jim

### #3 SuperSlow

Reputation: 0
• Posts: 16
• Joined: 16-October 12

## Re: Calculating percentages

Posted 11 February 2014 - 10:23 PM

I am not sure why it is working now. But thank you.
I had used decimal numbers before and it wasn't working for me.
You have the magic touch my good sir.