Thread: cout help!

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97

    cout help!

    Okay so I am new to c++ and have a quick question. After looking at the code below I don't understand how to print x to 17.00. I know they are sticky flags I keep using them to demonstrate but why does the 2nd or 3rd line of code not produce 17.00 they produce the output below the code. HELP!!!!
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main(void)
    {
    	double x = 17;
    
    	std::cout << x << std::endl;
    	std::cout << std::setprecision(2) << x << std::endl;
    	std::cout << std::showpoint << std::setprecision(2) << x << std::endl;
    	std::cout << std::noshowpoint << std::setprecision(2) << x << std::endl;
    
    	return 0;
    }
    Code:
    17
    17
    17.
    17
    Press any key to continue...

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    You've only told it to use as much precision as it needs so long as it only needs two or less.

    If you want a fixed format you should probably read the documentation.

    Soma

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Quote Originally Posted by phantomotap View Post
    You've only told it to use as much precision as it needs so long as it only needs two or less.

    If you want a fixed format you should probably read the documentation.

    Soma
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ? with cin and cout
    By Limming in forum C++ Programming
    Replies: 3
    Last Post: 08-18-2007, 11:32 AM
  2. std::cout or using namespace std or using std::cout
    By ComDriver in forum C++ Programming
    Replies: 13
    Last Post: 01-31-2005, 11:54 AM
  3. Whats the difference between cout and std::cout?
    By mdshort in forum C++ Programming
    Replies: 10
    Last Post: 12-30-2003, 05:34 PM
  4. cout
    By Moffesto in forum C++ Programming
    Replies: 9
    Last Post: 06-20-2002, 01:42 PM
  5. cout and cin
    By ygfperson in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2002, 10:32 AM