### Temperature Conversion Program

Hi all beginner hear and im having trouble with the program im writing, below is my code.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677`` ``````#include #include #include #include using namespace std; void description(); char temperatureScale(char A); double temperatureReading(double &num); void convertCtoF(); void convertFtoC(); void Results(); char tempChoice; double temperature; float Ftemp, Ctemp, resultF, resultC; int main() { description(); char choice; choice = temperatureScale(choice); double num; temperatureReading(num); convertFtoC(); convertCtoF(); Results(); } void description() { cout<<"This program will convert a temperature reading provided in " <>tempChoice; } double temperatureReading(double &num) { cout<<"Please enter your temperature reading (in degrees): "; cin>>temperature; } void convertFtoC() { float Ctemp; Ctemp = (temperature - 32)/1.8; } void convertCtoF() { float Ftemp; Ftemp = 1.8 * temperature +32; resultF = Ftemp; } void Results() { cout<<"Your temperature reading converts as follows: " <

It needs to output like this:

Fahrenheit: (based on what scale the user selects)
Celcius: (based on the scale the user selects)
------
If the user selects Fahrenheit then the value they enter should just get put in for the results and the only calculation you will get is Celsius and vice versa for the Celsius data.

When I run my program i get this:

This program will convert a temperature reading provided in
either Fahrenheit or Celcius to the other measurment scale.

What temperature scale did you use (F = Fahrenheit; C = Celsius)? F

Fahrenheit: 413.6
Celsius: 100

Please help as you can see if i use Fahrenheit my calculation is right but my celsius isnt and vce versa for inputing C

Thanks.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include using namespace std; void description(); void temperatureScale(char &); void convertCtoF(); void convertFtoC(); int main() { description(); char choice; temperatureScale(choice); if (choice=='c' || temp=='C') convertCtoF(); else convertFtoC(); return 0; } void description() { cout<<"This program will convert a temperature reading provided in " <>A; } void convertFtoC(); { //declare your temp, ask for the value in celsius and compute this, display your result from here //no extra function is needed. } void convertCtoF(); { //declare your temp, ask for the value in Fahrenheit and compute this, display your result from here //no extra function is needed too. } ``````
Last edited on
is this what you mean?

 ``1234567891011121314151617181920212223242526`` ``````void convertFtoC() { float Ctemp; Ctemp = (temperature - 32)/1.8; resultC = Ctemp; if (tempChoice == F) { cout<<"Your temperature is converted as follows: " <

I get a wierd error when i do it that way, I need both the Fahrenheit and Celsius to output at the same time.
Where is the value of temperature in line 4 and 17? Whats with line 5, 18 anyway.
edit
my bad, i didn't know you want both at the same time. Accept the temperature from the user, send it to both functions convertFtoC and convertCtoF from main and now you can exclude the if part. i.e.
 ``12345678`` ``````convertCtoF(double temperature) { //yourcode here } convertFtoC(double temperature) { //yourcode here }``````
Last edited on
I am almost there, thanks for all your help so far.

When i run my code I get this:

This program will convert a temperature reading provided in
either Fahrenheit or Celcius to the other measurment scale.

What temperature scale did you use (F = Fahrenheit; C = Celsius)? c
Your temperature is converted as follows:
Fahrenheit: -2.24605e+307
Celsius: -Inf

Why the weird calculations?
temperature is what the user enters, just to answer your last question better
Can you show me all your code? I need to know where we're flying blind.
Last edited on
Here is is from line 1

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980`` ``````#include #include #include #include using namespace std; void description(); char temperatureScale(char &); double temperatureReading(double num); void convertCtoF(double temperature); void convertFtoC(double temperature); void Results(); char tempChoice; double temperature, num; float Ftemp, Ctemp, resultF, resultC; int main() { description(); char choice; choice = temperatureScale(choice); if (choice == 'c' || tempChoice == 'C') { temperatureReading(num); double temperature; convertCtoF(temperature); } else { temperatureReading(num); double temperature; convertFtoC(temperature); } return 0; } void description() { cout<<"This program will convert a temperature reading provided in " <>A; } double temperatureReading(double num) { cout<<"Please enter your temperature reading (in degrees): "; cin>>temperature; } void convertFtoC(double temperature) { float Ctemp; Ctemp = (temperature - 32)/1.8; cout<<" " <

And the result of that code are the weird values i showed before
You're complicating things for yourself.

On line 9, you don't need to have temperatureScale as char, just leave it as void temperatureScale(char &);[/code] and on line 25, you don't need to assign temperatureScale(choice) to choice i.e. choice=temperatureScale(choice), just say temperatureScale(scale).
You don't need line 26 to 37 anymore, just replace them with convertCtoF(temperature) and convertFtoC(temperature)
i.e.
 ``12345678`` ``````//after temperatureScale(choice); //just do double temperature; temperatureReading(temperature); convertCtoF(temperature); convertFtoC(temperature); return 0; }``````

whereas you have
 ``12345`` ``````void temperatureReading(double &num) { cout<<"Enter the temperature: "; cin>>num; }``````
I replaced everything as you suggested and this is what my code now looks like:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` ``````#include #include #include #include using namespace std; void temperatureScale(char &); double temperatureReading(double num); void convertCtoF(double temperature); void convertFtoC(double temperature); void description(); char tempChoice; double temperature, num; float Ftemp, Ctemp, resultF, resultC; int main() { description(); char choice; temperatureScale(choice); double temperature; temperatureReading(temperature); convertCtoF(temperature); convertFtoC(temperature); return 0; } void description() { cout<<"This program will convert a temperature reading provided in " <>temperature; } void convertFtoC(double temperature) { float Ctemp; Ctemp = (temperature - 32)/1.8; cout<<" " <

And now when I output is both convertFtoC and convertCtoF show the cout lines....hmm...
OKAY! let's finish this once and for all
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include #include using namespace std; void temperatureReading(double &); void convertCtoF(double temperature); void convertFtoC(double temperature); void description(); int main() { double temperature; description(); temperatureReading(temperature); convertCtoF(temperature); convertFtoC(temperature); return 0; } void description() { cout<<"This program will convert a temperature reading provided in " <>num; } void convertFtoC(double temperature) { float Ctemp; Ctemp = (temperature - 32)/1.8; cout<<"Fahrenheit: " <

You can find my sample input here: http://codepad.org/IRJqQsTW
I had to change line 29 because the compiler on the website cannot make use of cin, so I assumed the user entered 20.
Last edited on
I'm afraid I am still receiving unorthodox answers to both calculated values my code is very very similar to the code you just supplied me with,
The code works, but I get a weird value for an answer and i am unsure why, anyone have any suggestions?
Topic archived. No new replies allowed.