Thread: Calculating a percentage

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    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
    }
    Double Helix STL

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    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.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    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
    Double Helix STL

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    [edit]sry, i didnt c it in desolations post[/edit]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Percentage Complete?
    By Abda92 in forum C++ Programming
    Replies: 16
    Last Post: 09-21-2007, 03:04 PM
  2. Calculating : high numbers
    By MiraX33 in forum C++ Programming
    Replies: 9
    Last Post: 06-08-2006, 11:08 PM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. Percentage
    By skyglin in forum C Programming
    Replies: 7
    Last Post: 06-23-2003, 05:18 PM
  5. Taking input while calculating
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 04:47 PM