locked
Need help with my C++ program RRS feed

  • Question

  • Hi.

    I am trying to design a program that asks user to input the water temperature using a decimal number and the units (C for celsius or F for Farenheit). If the input temperature is in Fahrenhiet, it converts the result to Celsius. Depending on the temperature, different messages are printed about whether water is a gas, liquid, solid or at freezing or boiling point. If the unit entered is not C or F the program prints out a message and ends.

    I tried making the following source code:

    #include<iomanip>

    #include<iostream>

    using namespace std;

     

    int main()

    {

    //Declaration statements    

    int user_temp, celsius_temp;

    bool unit;

        double;

     

    //print your name, student number and date

    cout<< "Name:[put your name here]\n" <<endl;

    cout<< "Student Number:[put today's date here]\n" <<endl;

    cout<< "Date:[put today's date here]\n"<<endl;

    cout<< endl;

     

    cout<< "Enter water temperature:";

    cin>>user_temp;

     

     

    cout<<"Enter C(for celsius) or F(for Farenheit:)";

    cin>>unit;

    if (unit=='F' && unit=='C')

    {

    cout<< "Please C or F"<<endl;

    }

    else if (unit == 'F')

    {

    celsius_temp==(user_temp - 32)*5/9;

    }

    else if (unit == 'C')

    {

    celsius_temp == user_temp;

       cout<<fixed<<setprecision(4);

       cout<<"water temperature is"<<celsius_temp<<"Celsius"<<endl;

    }

    if(celsius_temp==0)

    {

    cout<<"Water starts to freeze at this temperature"<<endl;

    }

    else if(celsius_temp==100)

    {

    cout<<"Water starts to boil at this temperature"<<endl;

    }

    else if(celsius_temp<0)

    {

    cout<<"Water is a solid at this temperature"<<endl;

    }

    else if(celsius_temp>0)

    {

    cout<<"Water is a gas at this temperature"<<endl;

    }

    else if(celsius_temp>0 && celsius_temp<100)

    {

    cout<<"Water is a liquid at this temperature"<<endl;

    }

     

    cout<<endl;

    system ("pause");

    return 0;

    }

     

    Now, I dont know what the problem is, but the code is not working fine. Also I am not too sure about the formula for converting Fahrenheit to Celsius.

    Any help will be appreciated.

    Sunday, February 20, 2011 9:41 PM

Answers

  • >the code is not working fine.

    You need to be much more specific. What *exactly*
    is not "fine" about it?

    >if (unit=='F' && unit=='C')

    How could unit be both F and C at the same time?

    >bool unit;

    A bool variable can only be true or false. It can't
    be used to hold a character.

    >I am not too sure about the formula for converting
    >Fahrenheit to Celsius.

    A Google/Yahoo/Bing search shouldn't be too hard.

    http://lmgtfy.com/?q=fahrenheit+to+celsius+formula

    - Wayne
    • Proposed as answer by D.Sreedhar Wednesday, February 23, 2011 6:44 AM
    • Marked as answer by Jesse Jiang Tuesday, March 1, 2011 3:02 AM
    Monday, February 21, 2011 1:04 AM
  • JockyMc wrote:

    Code should go something like

    if (unit == 'F')
    {
    celsius_temp = (user_temp - 32) / (5/9);

    Careful. (5/9) == 0, since you are performing integer division. Make  it

    celsius_temp = (user_temp - 32) * 9 / 5;


    Igor Tandetnik

    • Marked as answer by Jesse Jiang Tuesday, March 1, 2011 3:02 AM
    Wednesday, February 23, 2011 3:04 PM

All replies

  • >the code is not working fine.

    You need to be much more specific. What *exactly*
    is not "fine" about it?

    >if (unit=='F' && unit=='C')

    How could unit be both F and C at the same time?

    >bool unit;

    A bool variable can only be true or false. It can't
    be used to hold a character.

    >I am not too sure about the formula for converting
    >Fahrenheit to Celsius.

    A Google/Yahoo/Bing search shouldn't be too hard.

    http://lmgtfy.com/?q=fahrenheit+to+celsius+formula

    - Wayne
    • Proposed as answer by D.Sreedhar Wednesday, February 23, 2011 6:44 AM
    • Marked as answer by Jesse Jiang Tuesday, March 1, 2011 3:02 AM
    Monday, February 21, 2011 1:04 AM
  •  

    Hi Muzammil,

     

    Additionally, the value "unit" should be char, but in your codes it was bool, when we run at this line "cin>>unit;"  it will get error.

     

    I hope my suggestions can help you to solve this problem.

     

    Regards!

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 22, 2011 5:49 AM
  • >Additionally, the value "unit" should be char,
    >but in your codes it was bool,

    I already pointed out that error:

    >>bool unit;

    >A bool variable can only be true or false. It can't
    >be used to hold a character.


    >when we run at this line "cin>>unit;"  it will get error.

    Not an error, but an automatic conversion. A zero will set
    unit to false, any other value will set it to true.

    - Wayne
    Tuesday, February 22, 2011 6:02 AM
  •  

    Hi WayneAKing,

     

    I'm sorry for that I haven't seen your reply carefully.

     

    Thank for your contribution for this forum.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 23, 2011 7:46 AM
  • Code should go something like

    if (unit == 'F') 
    {
      celsius_temp = (user_temp - 32) / (5/9);
    }
    else if (unit == 'C')
    {
      celsius_temp = user_temp;
    }
    else
    {
      // error. must choose F or C
    }

    Wednesday, February 23, 2011 3:01 PM
  • JockyMc wrote:

    Code should go something like

    if (unit == 'F')
    {
    celsius_temp = (user_temp - 32) / (5/9);

    Careful. (5/9) == 0, since you are performing integer division. Make  it

    celsius_temp = (user_temp - 32) * 9 / 5;


    Igor Tandetnik

    • Marked as answer by Jesse Jiang Tuesday, March 1, 2011 3:02 AM
    Wednesday, February 23, 2011 3:04 PM
  •  "Igor Tandetnik [MVP]" wrote in message news:926b6931-b97a-4efa-af66-432195c921fa@communitybridge.codeplex.com...

    Careful. (5/9) == 0, since you are performing integer division. Make  it
    celsius_temp = (user_temp - 32) * 9 / 5;

    In the dark ages we used to use

    celsius_temp = ::MulDiv( user_temp - 32, 9, 5 );

    If I remember right it handles rounding to the nearest integer.

    Dave

    -- David Webber
    Mozart Music Software
    http://www.mozart.co.uk
    For discussion and support see
    http://www.mozart.co.uk/mozartists/mailinglist.htm


    David Webber Author of Mozart music notation software http://www.mozart.co.uk
    Saturday, March 5, 2011 11:16 PM