Thread: trivial: precision

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    trivial: precision

    i have some trouble formatting output to the console.

    my desired outcome: 14.44 (two decimal places)

    i am writing a simple program that converts a fahrenheit temperature into a celsius temperature, but i cannot get the output to print the decimal places. i dont know how to use the 'setf' and the 'precision' functions...help...
    eat, drink and be merry. for tomorrow: we party.

  2. #2
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    use iomanip.h if you have it.
    think only with code.
    write only with source.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    got it

    thanks, i finally got it working.

    but i do have another question. let's say that i wanted to print two strings on the same line, one right justified and one left justified...kind of like a table output, sans the borders and such...is that possible? i'm trying it right now to see if my compiler is going to choke....

    doomo.
    eat, drink and be merry. for tomorrow: we party.

  4. #4
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    wait, do you mean you want to align the output or after the temperature conversion, you can not see the decimals?

    if you have set your variables to "int", change them to "long double".

    maybe this is the problem you have.
    think only with code.
    write only with source.

  5. #5
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    is this supposed to be a window console (has windows support) or a dos program you're making?

    a default console should have 80 columns and 25 rows if this helps.
    Last edited by toaster; 08-05-2002 at 05:58 PM.
    think only with code.
    write only with source.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    28
    dos, definitely dos...

    alright, so here's what i want it to look like:

    temperature in F temperature in C
    0 -17.77
    .. ..
    100 37.78

    i want to know if it's possible to somehow align the text, or at least format the output, so that it has some kind of standardization to it....
    Last edited by breanne; 08-05-2002 at 06:31 PM.
    eat, drink and be merry. for tomorrow: we party.

  7. #7
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    //using MSVC++ 6.0

    just change the "20" in the for loop to a "100".
    the "20" is used in the for loop to show you how the "temperature in F" and "temperature in C" are arranged.
    I hope the formulas are correct.

    Code:
    /*cheap method: do this by the use of tabs*/
    
    #include<iostream.h>
    
    double FtoC(double temp) // convert from F to C
    {
     return((temp-32.0)/1.8);
    }
    
    double CtoF(double temp) // convert from C to F
    {
     return((temp*1.8)+32.0);
    }
    
    int main(void)
    {
     int x=0;
     cout<<"temperature in F"<<"\t"<<"temperature in C"<<endl;
     for(x=0;x<=20;x++) //212_F=100_C
     {
      cout<<x<<"\t\t\t"<<double(FtoC(x))<<endl;
     }
     return 0;
    }
    Code:
    /*using cout class*/
    
    #include<iostream.h>
    #include<iomanip.h>
    
    double FtoC(double temp) // convert from F to C
    {
     return((temp-32.0)/1.8);
    }
    
    double CtoF(double temp) // convert from C to F
    {
     return((temp*1.8)+32.0);
    }
    
    int main(void)
    {
     int x;
     cout<<"temperature in F"<<"\t"<<"temperature in C"<<endl;
     for(x=0;x<=20;x++) //212_F=100_C
     {
      cout.width(16); // refer to comment one
      cout<<x;
      cout<<"\t";
      cout.width(16); // refer to comment one
      cout<<double(FtoC(x))<<endl;
     }
     return 0;
    }
    
    /* comment one:
     the argument value in the cout class member function width() is set to 16 because both "temperature in F" and "temperature in C" are 16 characters long.
    */
    have fun.

    you can also add in argc and *argv to main for a specific temperature conversion.
    Last edited by toaster; 08-05-2002 at 07:16 PM.
    think only with code.
    write only with source.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    I dug deep, Breanne.
    Code:
    std::cout << setw(x) << right << something << setw(y) << left << somethingElse;
    This works on Borland.

    The correct methodology would be:
    Code:
     
    std::cout.setf(ios::right);
    std::cout << something;
    std::cout.unsetf(ios::right);
    Actually, the justification depends on the data-type. As I recall (gentle and respectful flaming allowed) char/string data is left-justified, by default, and numeric data is right-justified, again, by default. (Don't quote me on this, though. [Disclaimer] )

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    oh my.

    Gods you two are, Gods i tell you.

    doomo very much, now i can take a nice refreshing shower and take my eyes off the screen for a second or three.
    eat, drink and be merry. for tomorrow: we party.

  10. #10
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: oh my.

    Originally posted by breanne
    shower
    damnit thats what i forgot today
    hello, internet!

  11. #11
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161

    Re: oh my.

    Originally posted by breanne
    Gods you two are, Gods i tell you.

    doomo very much, now i can take a nice refreshing shower and take my eyes off the screen for a second or three.
    oh my, how flattered I am! please, don't call me that but you can call skipper whatever skipper pleases to hear. a plain "thank you" is already too much for me. besides, a few minutes of my life isn't that much wasted.

    anyway, G_ds are supposed to be superior to others and don't make mistakes. I just found a mistake in my previous post.

    " just change the "20" in the for loop to a "100". "
    should be
    " just change the "20" in the for loop to "212". "

    I forgot I was dealing with F -> C conversion. I was thinking the opposite.

    [ self note: never watch television while programming ]
    Last edited by toaster; 08-05-2002 at 09:14 PM.
    think only with code.
    write only with source.

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    please, don't call me that but you can call skipper whatever skipper pleases to hear.
    We can just stick with Skipper. I've got a fair share of boo-boos out here myself.

    -Skipper
    "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. Setting Precision Confusion
    By dnguyen1022 in forum C++ Programming
    Replies: 11
    Last Post: 01-14-2009, 10:38 AM
  2. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  3. Replies: 1
    Last Post: 04-03-2008, 01:17 AM
  4. Precision based floating-point
    By Mario F. in forum C++ Programming
    Replies: 4
    Last Post: 07-17-2006, 10:35 AM
  5. International Limits on Precision
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-20-2004, 06:32 AM