# Calculating a percentage

• 11-24-2006
swgh
Calculating a percentage
I have to a small assignment, and I am stoked on the last part. I basiclly have
to find 9 percent of any number entered by the user and then add 200 to it.
I will not post the assignment code cause its huge but I have created a small
dummy program to see if I can get the calculation correct. I have never been good at percentage calculations lol.

This is my best effort, it compiles but its the wrong output. What changes do I have to do to make the output correct?

EDIT: Read the comment above the static_cast for an idea.

Code:

#include <iostream>

using std::cout;
using std::endl;
using std::cin;
using std::fixed;

#include <iomanip>

using std::setprecision;

// main function - driver
int main ( void )
{
double sales; // total sales made for the week
double salary; // salary for each employee

cout << "Enter sales in pounds ( enter -1 to quit ): ";
cin >> sales;

while ( sales != -1 )
{
// calculate salary- each employee
// gets 200 standard and 9% of sales
salary = static_cast < double > ( sales ) / 0.9 + 200;

cout << "\nSalary is: " << setprecision( 2 ) << fixed << salary;

cout << "\n\nEnter sales in pounds ( enter -1 to quit ): ";
cin >> sales;
}

cin.get(); // freeze console output window

return 0; // indicate program ended sucsesfuly
}

• 11-24-2006
Desolation
First, you are casting 'sales' to double and it's already a double so that's a completely useless cast. Second, you should multiply by 0.09 (not 0.9, this is 90%) instead of dividing.
• 11-24-2006
swgh
Doh! Thanks Desolation!

I think I need to back to primary school!
I just noticed about the cast, I thought i delcared sales as an int. I got rid of the cast and it worked like a charm. Thanks for the help
• 11-24-2006
h_howee
sry, i didnt c it in desolations post[/edit]