Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   C++ (https://www.askmehelpdesk.com/forumdisplay.php?f=439)
-   -   C++ programming Qs (beginner) (https://www.askmehelpdesk.com/showthread.php?t=132759)

  • Sep 23, 2007, 07:37 AM
    Harris2007
    C++ programming Qs (beginner)
    Hi there... I am a novice in C++ programming. I wrote a very simple 'temperature conversion' program that asks user to input whether they want a Celsius to Fahrenheit conversion, or the opposite, and then calls the respective function (i.e. either a Celsius to Fahrenheit converter OR a Fahrenheit to Celsius converter). When I run the program, only the first function (i.e. C to F conversion) runs. Can you please let me know what I'm doing wrong? The code is as follows. Thanks for your help!!

    # include <iostream>
    # include <cmath>
    void cfconverter (double);
    void fcconverter (double);

    int main ()
    {
    using namespace std;
    double t;
    int decide;
    cout<<"Temperature Converter\n\n";
    cout<<"Do you want C-to-F [1] OR F-to-C [2] conversion? Enter '1' or '2'";
    cin>>decide;
    cout<<"Enter temperature: ";
    cin>>t;
    if (decide=1) {cfconverter(t);}
    else {fcconverter(t);}
    return 0;
    }

    void cfconverter (double t)
    {
    using namespace std;
    double f;
    f=(1.8*t)+32;
    cout<<"\nA temperature of "<<t<<" in Celsius = "<<f<<" in Fahrenheit\n";
    }

    void fcconverter (double t)
    {
    using namespace std;
    double c;
    c=(t-32)/1.8;
    cout<<"\nA temperature of "<<t<<" in Fahrenheit = "<<c<<" in Celsius\n";
    }
  • Sep 23, 2007, 12:10 PM
    asterisk_man
    common problem :)

    should be

    (decide==1) instead of (decide=1)

    = is assignment, == is comparison

    hope this helps!

  • All times are GMT -7. The time now is 08:39 PM.