Limited output

This is a discussion on Limited output within the C++ Programming forums, part of the General Programming Boards category; Say I have a double wich has a value of 4.567 How can let the output, using cout, be 4.57 ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    Limited output

    Say I have a double wich has a value of 4.567
    How can let the output, using cout, be 4.57 or 4.6 ?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
      double d =  4.567;
    
      std::cout.precision ( 3 );
      std::cout<< d <<std::endl;
    
      std::cout.precision ( 2 );
      std::cout<< d <<std::endl;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    And how to make it output a 7 as 7.0?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >And how to make it output a 7 as 7.0?
    By using the std::fixed manipulator for cout. Or you could go here
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
      double d =  7;
    
      std::cout.precision ( 1 );
      std::cout<< std::fixed << d <<std::endl;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    I got: "fixed; undeclared identifier"

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I got: "fixed; undeclared identifier"
    Then you didn't use it correctly, review the code I gave you or do this:
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
      double d =  7;
    
      std::cout.precision ( 1 );
      std::cout.setf ( std::ios_base::fixed, std::ios_base::floatfield );
      std::cout<< d <<std::endl;
    }
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    I want to use it in a function in a class, that makes any difference?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I want to use it in a function in a class, that makes any difference?
    No, not really. It would be pretty dumb to allow output formatting in global functions but not member functions. Perhaps if you posted the code that is giving you problems I could tell you what isn't working like you would expect.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 06:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

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