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;
}

Actually I'm surprised that you get any answer at all, considering the syntax errors in this code:

cout - is only used at the beginning of the statement not in the middle of it

if you're going to specify a return type then you must return a value of that type before the function exits.

Once these are fixed, it appears that your problem might be related to using an int variable in a calculation and casting it to a float. Remember that floats aren't very precise, you might be better off making everything double's. I've made some changes to your code and both functions seem to give correct answers:

double convertCelsiusToFahren() {
    double celsius = 0;
    double fahren = 0;
    cout << "\nEnter temperature in celsius :";
    cin >> celsius;
    fahren = ((celsius * 9) / 5) + 32;
    cout << "\nEquivalent of " << celsius << " Degree Celsius equals " << fahren  << " Degrees in Fahrenheit.\n";
    return fahren;
}
//FUNCTION BODY
double convertFahrenToCelsius() {
    double celsius = 0;
    double fahren = 0;
    cout << "\nEnter temperature in fahrenheit :";
    cin >> fahren;
    celsius = (fahren - 32) * 5 / 9;
    cout << "\nEquivalent of " << fahren << " Degree Fahrenheit equals " << celsius << " Degrees in Celsius.\n";
    return celsius;
}

On a side note, getting each function to ask for input, leads to a lot of repetition in your code. You should break that part out of the functions and either leave them in main or create new functions for them:

void GetInput(string from, string to,double& fromTemp, double& toTemp)
{
        cout << "\nEnter temperature in " << from << " :";
        cin >> fromTemp;
        cout << "\nEnter temperature in " << to << " :";
        cin >> toTemp;
}

Actually I'm surprised that you get any answer at all, considering the syntax errors in this code

C/C++ gives you A LOT of rope to hang yourself with. It can be absolutely deadly. It would be nice if it would give you an error when you have no return for a non-void function, but instead it gives a warning and will compile. In this case, it seemed to cause no problems, at least on my machine, but a note to the OP and anyone else, you need to read the compiler warnings carefully. If you see a warning like "no return statement in function returning non-void", be aware that that is "undefined behavior". Undefined behavior in C++ means just that. Just about anything can happen. In a program like this, it's pretty safe, though things can get very very weird, so fix that warning and recompile. But I'm not that surprised that it compiled and ran.

As for the gibberish you got, that was because you had "cout" in the middle of your "cout" statement. You only want it in the front of your statement. But again, it DOES compile, it just does something that you clearly did not intend because it didn't know what you intended. Again, lots of rope to hang yourself with in C++. In this case, no big deal.

As mentioned previously, you also don't need to display stuff like this: << int(celsius) <<
Just do this: << celsius <<

And it really shouldn't be defined as an integer. Define both as floats (or doubles as mentioned above for more accuracy). When you do arithmetic with integers, you have to be careful because you can get some pretty serious round-off errors that you may not have intended. Making everything a float or a double prevents that in calculations like this.

#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 = (1.8 * celsius) + 32;
    cout << "\nEquivalent of " << celsius << " Degree Celsius equals " << fahren << " Degrees in Fahrenheit." << endl;
}
//FUNCTION BODY
float convertFahrenToCelsius() {
    float fahren = 0;
    int celsius = 0;
    cout << "\nEnter temperature in fahrenheit :";
    cin >> fahren;
    celsius = (fahren -32) * 0.555;
    cout << "\nEquivalent of " << fahren << " Degree Fahrenheit equals " << celsius  << " Degrees in Celsius." << endl;
}

Compile your code Mabdullah. Note the warnings: "no return statement in function returning non-void"

OP, you must either return a value from a function or you need to make it a void function. You may sometimes get away with it if you do not, but often you will not get away with it. See tinstaafl's post.

commented: Sorry,I am using Dev-C++ and it works perfectly.Anyway Thanks. +0

Thank you guys for your feedback, especially tinstaafl 747 and AssertNull. I think I rushed to post without analyzing my code properly for errors myself. On breaking the input part into a seperate function as suggested by tinstaafl 747, I will try that part next. .Thank you again for your time.

Thank you guys for your feedback, especially tinstaafl 747 and AssertNull. I think I rushed to post without analyzing my code properly for errors myself. On breaking the input part into a seperate function as suggested by tinstaafl 747, I will try that part next. .Thank you again for your time.

You are welcome. And may I comment on how refreshing it is to see someone self-teaching themselves and clearly putting in some effort to try to understand what they are doing, and experimenting. You also took the time to try to make a guess on what you thought might be happening. Your guess was not correct, but the point is that you made one. Far too many people want to learn C++ and other languages without experimenting and without struggling through the process. And far, far, far too many people on this forum are simply taking Freshman Computer Programming classes, posting the assignment verbatim, with NO effort at all. Your thread is a refreshing exception to this and it's depressing that it IS the exception.

As far as my comments about undefined behavior and weird results, it's important to note that sometimes if you make a mistake, you'll get really weird results, sometimes you'll get away with it for a while, and sometimes you will get away with it for a while, then make a slight change to your code having nothing to do with the bug and the piper will be paid THEN. Naturally you'll go back and look at your RECENT changes and be puzzled when you decide that there's no reason that would result in the weird results, yet you'll study that code for hours, then have your more knowledgeable friends look at it, THEY'll say it's fine, and all along it was code that was buggy, but you got away with months ago, then changed slightly.

Your program is a perfect example of this. You have a non-void function and did not return anything from it. In this case you got away with it. However, if you tried to assign a variable to equal that function's returned value, all sorts of weird stuff could happen. That said, however, don't be afraid to experiment. I always warn people that undefined behavior can do anything, including delete your hard drive (i.e. the example I wrote at the bottom of this thread) and occasionally accidentally scare them:

https://www.daniweb.com/programming/computer-science/threads/504117/integer-pre-increment-addition-operator-precedence-problem#post2202679

However, you are experimenting with "Hello World", converting temperatures, counting to ten, etc., not writing code to delete your hard drive, so you're not going to actually hurt anything, so experiment away.

Finally, keep in mind that "int main" itself is a function that needs to return a value. That value should be 0 upon successful completion of the program, so stick return 0; between lines 35 and 36 of your original program.

If this question is answered, please remember to mark this solved. Also when you have new questions please start a new post. This helps others when they're searching for answers as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.