2 Replies - 212 Views - Last Post: 11 February 2014 - 10:23 PM Rate Topic: -----

#1 SuperSlow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 SuperSlow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1