PDA

View Full Version : C++ programming Qs (beginner)


Harris2007
Sep 23, 2007, 07:37 AM
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";
}

asterisk_man
Sep 23, 2007, 12:10 PM
common problem :)

should be

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

= is assignment, == is comparison

hope this helps!