print floating-points

This is a discussion on print floating-points within the C++ Programming forums, part of the General Programming Boards category; I have copied the below example into my compiler from a book. The example output it shows is: How many ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    print floating-points

    I have copied the below example into my compiler from a book. The example output it shows is:
    How many departments are there in the company? 7
    How many employees are in the company? 45
    What is the company's total payroll? 7645.32

    The average number of employees per department is 6
    The average payroll per department is $1092.19
    When I compile the program it gives me scientific notation for the average payroll per department instead of 1029.19. Why's that?
    I'm using the Borland C++ 4.5 complier

    Code:
    // Filename: DIV.CPP
    
    #include <iostream.h>
    #include <iomanip.h>
    
    int main()
    {
       int numDept, totalEmp, avgDept;
       float avgPay, pay;
    
       cout <<"How many departments are there in the company? ";
       cin >> numDept;
       cout <<"How many employees are there in the company? ";
       cin >> totalEmp;
       cout <<"What is the company's total payroll? ";
       cin >> pay;
       avgDept = totalEmp / numDept;   // Integer division
       avgPay = pay / numDept;   // Floating-point division;
       cout <<"\nThe average number of per department employees "
    	"is " << avgDept << "\n";
       cout.setf(ios::showpoint);
       cout <<"The average payroll per department is $"
    		  << setprecision(2) << avgPay;
    
       return 0;
    }
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    You also need cout << fixed;. 'fixed' allows decimals to be displayed rather than scientific notation.

    -Skipper

    P.S. setprecision(2) will give you a displayed value of 1.2E+03 for the value of '1200'. It's the '1.2' that has had its "precision" set to '2'. Had setprecision() had an argument of '3', you would display 1.20E+03 instead.
    Last edited by skipper; 12-01-2002 at 09:45 AM.
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! calculate arbitrary number of floating points
    By cakestler in forum C Programming
    Replies: 5
    Last Post: 02-26-2009, 02:47 PM
  2. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 09:14 PM
  3. print output problem
    By chor in forum C Programming
    Replies: 1
    Last Post: 12-29-2002, 09:45 AM
  4. Replies: 1
    Last Post: 07-31-2002, 12:35 PM
  5. Floating points and Double output
    By bman1176 in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2001, 01:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21