a "temp conversion" programme

Discussion in 'C++' started by arnuld, Mar 5, 2007.

  1. arnuld

    arnuld Guest

    i have created a new temperature conversion programme. it converts a
    temperature from Fahrenheit to Celsius or from Celsius to Fahrenheit
    at user's will. tell em if i need some improvements:

    // conversion of temperature
    // using Centigrade and Fahrenheit

    #include <iostream>

    int main() {
    double centi;
    double fahr;
    char ans;

    std::cout << "do you want to convert a Centigrade into Fahrenheit:
    [y/n]";
    std::cin >> ans;

    if(ans == 'y')
    {
    std::cout << "Enter temperature in Centigrade: ";
    std::cin >> centi;

    fahr = (9.0/5.0 * centi) + 32;
    std::cout << centi
    << " centigrade = "
    << fahr
    << " fahrenheit"
    << "\n";
    }

    else
    {
    std::cout << "Enter temperature in Fahrenheit: ";
    std::cin >> fahr;

    centi = 5.0/9.0 * (fahr - 32);
    std::cout << fahr
    << " farenheit = "
    << centi
    << " centigrade"
    << "\n";
    }

    return 0;
    }

    -- arnuld
    http://arnuld.blogspot.com
    arnuld, Mar 5, 2007
    #1
    1. Advertising

  2. arnuld wrote:
    > i have created a new temperature conversion programme. it converts a
    > temperature from Fahrenheit to Celsius or from Celsius to Fahrenheit
    > at user's will. tell em if i need some improvements:
    >
    > // conversion of temperature
    > // using Centigrade and Fahrenheit
    >
    > #include <iostream>
    >
    > int main() {
    > double centi;
    > double fahr;
    > char ans;
    >
    > std::cout << "do you want to convert a Centigrade into Fahrenheit:
    > [y/n]";
    > std::cin >> ans;
    >
    > if(ans == 'y')
    > {
    > std::cout << "Enter temperature in Centigrade: ";
    > std::cin >> centi;
    >
    > fahr = (9.0/5.0 * centi) + 32;
    > std::cout << centi
    > << " centigrade = "
    > << fahr
    > << " fahrenheit"
    > << "\n";
    > }
    >
    > else
    > {
    > std::cout << "Enter temperature in Fahrenheit: ";
    > std::cin >> fahr;
    >
    > centi = 5.0/9.0 * (fahr - 32);
    > std::cout << fahr
    > << " farenheit = "
    > << centi
    > << " centigrade"
    > << "\n";
    > }
    >
    > return 0;
    > }


    Well, you could format your output better...
    How about rounding to two digits after the decimal point?
    That way you wouldn't get values like "3.3333333333333"
    in your output.

    If you are interested in achieving this, #include
    the file 'iomanip' and look up on the manipulators
    'fixed' and 'setprecision'.

    HTH,
    - J.
    Jacek Dziedzic, Mar 5, 2007
    #2
    1. Advertising

  3. arnuld

    arnuld Guest

    > On Mar 5, 10:25 am, Jacek Dziedzic <> wrote:

    > Well, you could format your output better...
    > How about rounding to two digits after the decimal point?
    > That way you wouldn't get values like "3.3333333333333"
    > in your output.
    >
    > If you are interested in achieving this, #include
    > the file 'iomanip' and look up on the manipulators
    > 'fixed' and 'setprecision'.


    they are for "floats". i have tried that, you can not use them for
    "doubles"
    arnuld, Mar 5, 2007
    #3
  4. arnuld wrote:

    >> On Mar 5, 10:25 am, Jacek Dziedzic <>
    >> wrote:

    >
    >> Well, you could format your output better...
    >> How about rounding to two digits after the decimal point?
    >> That way you wouldn't get values like "3.3333333333333"
    >> in your output.
    >>
    >> If you are interested in achieving this, #include
    >> the file 'iomanip' and look up on the manipulators
    >> 'fixed' and 'setprecision'.

    >
    > they are for "floats". i have tried that, you can not use them for
    > "doubles"


    Try harder.

    #include<iostream>
    #include<iomanip>

    using namespace std;

    const double d=1.0/3.0;

    int main() {
    cout << fixed << setprecision(2) << d << ' '
    << setprecision(10) << d << endl;
    }

    HTH,
    - J.
    Jacek Dziedzic, Mar 5, 2007
    #4
  5. On Mar 5, 7:35 am, "arnuld" <> wrote:
    > > On Mar 5, 10:25 am, Jacek Dziedzic <> wrote:
    > > Well, you could format your output better...
    > > How about rounding to two digits after the decimal point?
    > > That way you wouldn't get values like "3.3333333333333"
    > > in your output.

    >
    > > If you are interested in achieving this, #include
    > > the file 'iomanip' and look up on the manipulators
    > > 'fixed' and 'setprecision'.

    >
    > they are for "floats". i have tried that, you can not use them for
    > "doubles"


    They should work for doubles too.

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 5, 2007
    #5
  6. arnuld

    arnuld Guest

    > On Mar 5, 12:06 pm, Jacek Dziedzic <>

    > Try harder.
    >
    > #include<iostream>
    > #include<iomanip>
    >
    > using namespace std;
    >
    > const double d=1.0/3.0;
    >
    > int main() {
    > cout << fixed << setprecision(2) << d << ' '
    > << setprecision(10) << d << endl;
    >
    > }



    Erm... and i was using "d.precision(3)".

    BTW, i removed "fixed" from your code. i does not make any difference.
    arnuld, Mar 5, 2007
    #6
  7. arnuld wrote:
    >> On Mar 5, 12:06 pm, Jacek Dziedzic
    >> <>

    >
    >> Try harder.
    >>
    >> #include<iostream>
    >> #include<iomanip>
    >>
    >> using namespace std;
    >>
    >> const double d=1.0/3.0;
    >>
    >> int main() {
    >> cout << fixed << setprecision(2) << d << ' '
    >> << setprecision(10) << d << endl;
    >>
    >> }

    >
    >
    > Erm... and i was using "d.precision(3)".
    >
    > BTW, i removed "fixed" from your code. i does not make any difference.


    Sure it does, you just didn't notice. I'll let you figure it out,
    though. Try this

    #include <iostream>
    #include <iomanip>

    using namespace std;

    int main() {
    cout << setprecision(3) << 123.456 << ' ' << fixed
    << setprecision(3) << 123.456 << endl;
    }

    - J.
    Jacek Dziedzic, Mar 5, 2007
    #7
  8. arnuld

    arnuld Guest

    > On Mar 5, 2:09 pm, Jacek Dziedzic <>

    > > BTW, i removed "fixed" from your code. i does not make any difference.

    >
    > Sure it does, you just didn't notice. I'll let you figure it out,
    > though. Try this
    >
    > #include <iostream>
    > #include <iomanip>
    >
    > using namespace std;
    >
    > int main() {
    > cout << setprecision(3) << 123.456 << ' ' << fixed
    > << setprecision(3) << 123.456 << endl;
    >
    > }


    yes, i noticed now. the output of your programme is: 123 123.456

    now here is some interesting pattern:

    1.)
    << setprecision(3) << 123.456
    << fixed
    << ' '
    << setprecision(3) << 123.456


    OUTPUT: 123 123.456

    here i have use /fixed/ as like a prefix

    2.)
    << fixed
    << setprecision(3) << 123.456
    << ' '
    << setprecision(3) << 123.456
    << endl;

    OUTPUT: 123.456 123.456

    here 1 /fixed/ worked for both.


    3.)
    << setprecision(3) << 123.456
    << ' '
    << setprecision(3) << 123.456
    << fixed
    << endl;

    }

    OUTPUT: 123 123

    /fixed/ doe snot work here.


    i want to know how exactly /fixed/ work, in context to patterns 1,2
    and 3.

    ?
    arnuld, Mar 5, 2007
    #8
  9. arnuld wrote:

    >> On Mar 5, 2:09 pm, Jacek Dziedzic
    >> <>

    >
    >> > BTW, i removed "fixed" from your code. i does not make any difference.

    >>
    >> Sure it does, you just didn't notice. I'll let you figure it out,
    >> though. Try this
    >>
    >> #include <iostream>
    >> #include <iomanip>
    >>
    >> using namespace std;
    >>
    >> int main() {
    >> cout << setprecision(3) << 123.456 << ' ' << fixed
    >> << setprecision(3) << 123.456 << endl;
    >>
    >> }

    >
    > yes, i noticed now. the output of your programme is: 123 123.456
    >
    > now here is some interesting pattern:
    >
    > 1.)
    > << setprecision(3) << 123.456
    > << fixed
    > << ' '
    > << setprecision(3) << 123.456
    >
    >
    > OUTPUT: 123 123.456
    >
    > here i have use /fixed/ as like a prefix
    >
    > 2.)
    > << fixed
    > << setprecision(3) << 123.456
    > << ' '
    > << setprecision(3) << 123.456
    > << endl;
    >
    > OUTPUT: 123.456 123.456
    >
    > here 1 /fixed/ worked for both.
    >
    >
    > 3.)
    > << setprecision(3) << 123.456
    > << ' '
    > << setprecision(3) << 123.456
    > << fixed
    > << endl;
    >
    > }
    >
    > OUTPUT: 123 123
    >
    > /fixed/ doe snot work here.
    >
    >
    > i want to know how exactly /fixed/ work, in context to patterns 1,2
    > and 3.


    One way would be to google for "fixed", "setprecision" and "manipulators".

    Consulting your favourite C++ textbook would be another.

    Anyway, fixed did not work in your last example, since it was
    output _after_ your numbers. They were output already, so it had
    no chance to act.

    Anyway 2, with 'fixed' the 'setprecision' manipulator tells the
    stream how many digits after the decimal point you want to output,
    without fixed it tells how many digits _total_ you want to output.

    HTH,
    - J.
    Jacek Dziedzic, Mar 5, 2007
    #9
  10. arnuld

    arnuld Guest

    > On Mar 5, 4:06 pm, Jacek Dziedzic <>


    > One way would be to google for "fixed", "setprecision" and "manipulators".



    i did that after reading your reply. sorry...

    i found one more manipulator named /scientific/ an opposite of /
    fixed/

    > Consulting your favourite C++ textbook would be another.


    yes, i tried Stroustrup before posting here my 3 patterns but it did
    not tell me anything useful, totally unlike Google.


    > Anyway, fixed did not work in your last example, since it was
    > output _after_ your numbers. They were output already, so it had
    > no chance to act.
    >
    > Anyway 2, with 'fixed' the 'setprecision' manipulator tells the
    > stream how many digits after the decimal point you want to output,
    > without fixed it tells how many digits _total_ you want to output.



    yes, i understood now, except one thing. from pattern '2' above, does
    one /fixed/ works for all subsequent /setprecision(s)/

    ?


    > HTH,


    of course that helped

    :)
    arnuld, Mar 5, 2007
    #10
  11. arnuld wrote:
    >> On Mar 5, 4:06 pm, Jacek Dziedzic <>

    >
    >
    >> One way would be to google for "fixed", "setprecision" and "manipulators".

    >
    >
    > i did that after reading your reply. sorry...
    >
    > i found one more manipulator named /scientific/ an opposite of /
    > fixed/


    Almost an opposite. In fact you have three modes -- the default
    (not fixed and not scientific), fixed and scientific.

    > yes, i understood now, except one thing. from pattern '2' above, does
    > one /fixed/ works for all subsequent /setprecision(s)/


    Yes, as long as they are on the same output stream.

    HTH,
    - J.
    Jacek Dziedzic, Mar 5, 2007
    #11
  12. arnuld

    arnuld Guest

    > On Mar 6, 1:28 am, Jacek Dziedzic <> wrote:
    > arnuld wrote:


    > > i found one more manipulator named /scientific/ an opposite of /
    > > fixed/

    >
    > Almost an opposite. In fact you have three modes -- the default
    > (not fixed and not scientific), fixed and scientific.
    >
    > > yes, i understood now, except one thing. from pattern '2' above, does
    > > one /fixed/ works for all subsequent /setprecision(s)/

    >
    > Yes, as long as they are on the same output stream.


    thanks Jacek
    arnuld, Mar 6, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    380
    Scott Allen
    May 5, 2005
  2. Nobber

    Diary Programme?

    Nobber, Nov 18, 2005, in forum: Java
    Replies:
    2
    Views:
    428
    Monique Y. Mudama
    Nov 18, 2005
  3. HAK
    Replies:
    0
    Views:
    335
  4. melledge
    Replies:
    0
    Views:
    620
    melledge
    Mar 9, 2005
  5. nyy
    Replies:
    3
    Views:
    115
    Dr John Stockton
    Sep 22, 2005
Loading...

Share This Page