very very simple c++ question

This is a discussion on very very simple c++ question within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm wondering why when I print the result in the super simple program it will not display only 2 ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    very very simple c++ question

    Hi, I'm wondering why when I print the result in the super simple program it will not display only 2 digits after the decimal point. What is the proper way of doing so? Thanks

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main ()
    
    {
    	int fah, i;
    	float celsius;
    	
        for (i = 1; i <= 4; i++)
    	{
    	cout << "Enter a Fahrenheit\n";
    	cin >> fah;
    
    	celsius = (5.0 / 9.0) * (fah - 32.0);
    	
    
    	cout  << "your answer is " << celsius << setiosflags(ios::fixed) << setiosflags(ios::showpoint) << setw(2)  << "  degrees celsius" << endl << endl;
    
    	
    	}
    	system ("pause");
    
    	return 0; 
    
    }

  2. #2
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    93
    setprecision

    Also, your program is telling cout to "print the value of celsius, then set ios::fixed, then..."
    Instead you need to set the flags/settings before you send the variable. In your case you would need to put "<< celsius" after "<< setw(2)" (or whatever combination you end up using), not before "<< setiosflags(ios::fixed)".
    C+/- programmer extraordinaire

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    I knew it would be something stupid like that! Thank you!

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    5
    I think you should use "<<setpresition(2)" before the veriable

    check the spilling of prestion.

    My Regards
    Salmi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

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