Hi there. I am new to cpp and self-taught. I tried playing with switch case and functions. But the output of this simple temp conversion program does not work as expected. I get wierd numbers in output. I even tried initializing the variables, still no use. I have indicated in comments at the top what I could be doing wrong, but not sure if thats the case. Could someone please look into this and let me know how i can get this working. Thank you.
/*
* SIMPLE TEMP CONVERSION PROGRAM USING SWITCH CASE AND FUNCTIONS
* Buggy as the conversion result returned makes no sense.
* I believe it has got something to do with using local variables inside functions
* and calling them out in main or returning back their value as a COUT
*/
#include <iostream>
using namespace std;
//FUNCTION DECLARATION
float convertCelsiusToFahren();
float convertFahrenToCelsius();
int main() {
int request = 0;
cout << "Simple Temperature Conversion Program\n";
cout << "=======================================\n\n";
cout << "Choose one of the below to convert \n\t1) Celsius to Fahrenheit \n\t\t OR\n\t2) Fahrenheit to Celsius? " << endl;
cin >> request;
switch (request)
{
case 1:
//FUNCTION CALL
convertCelsiusToFahren();
break;
case 2:
//FUNCTION CALL
convertFahrenToCelsius();
break;
default :
cout << "Unknown request. Program terminated!";
break;
}
}
//FUNCTION BODY
float convertCelsiusToFahren(){
int celsius = 0;
float fahren = 0.0;
cout << "\nEnter temperature in celsius :";
cin >> celsius;
fahren = ((celsius * 9)/5) + 32;
cout << "\nEquivalent of " << int(celsius) << cout << " Degree Celsius equals " << float(fahren) << cout << " Degrees in Fahrenheit." << endl;
}
//FUNCTION BODY
float convertFahrenToCelsius() {
float fahren = 0;
int celsius = 0;
cout << "\nEnter temperature in fahrenheit :";
cin >> fahren;
celsius = (fahren -32) *5 / 9;
cout << "\nEquivalent of " << float(fahren) << cout << " Degree Fahrenheit equals " << int(celsius) << cout << " Degrees in Celsius." << endl;
}