Problem with functions... I think

Started by
13 comments, last by KittyRa 17 years, 9 months ago
You're using functions in a wrong fashion. Don't put input/output code in them. Right now, you have functions that do both the calculations and the I/O, and their return values aren't used, because they do their own printing of the results. You should have FarhToCels() and CelsToFarh() that just do the conversions and handle I/O elsewhere, like in the main program. For example:

cout << "Insert Farhennheit: ";float Ftemp;cin>>Ftemp;float Ctemp=FarhToCels(Ftemp);cout<< Ftemp<<"F"<<" equals to "<<Ctemp<<"C"<<endl;
Advertisement
Also, you are working with floats in your functions. The functions should be returning floats, not ints. I believe you mentioned something about "loss of precision" earlier -- this is the reason for it. Floating point numbers are more precise than integers are. By returning an int, you are discarding the entire decimal portion of the number.

Rather than printing things in the functions, you should put those statements in the switch statement. This is more a matter of style than anything else, but I feel that it cleans the functions up a little and makes them easier to reuse. It also keeps them more focussed. Remember that all statements between { } count as one block. You can use these in your switch statement as follows:

switch(letter) {    case 'c':        {            cout << "Enter a celsius temperature . . .";            float celsiusVal;            cin >> celsiusVal;            cout << "In Fahrenheit: ";            cout << getCtemp(celsiusVal);        }        break;    case 'f':        {            cout << "Enter a fahrenheit temperature . . .";            float fahrenheitVal;            cin >> fahrenheitVal;            cout << "In Celsius: ";            cout << getFtemp(fahrenheitVal);        }        break;    default:        cout << "ERROR: You did not enter either \'c\' or \'f\'";}


You can see here that I've also given your functions parameters. The value is read in and then passed to the function. That way, if you change main at a later date (say to have just one input like 32 F), you don't have to worry about messing around with your functions; you just call the right one with the value.

EDIT: mikeman beat me to it.
EDIT 2: Actually, you don't need to put the statements in { }.
Quote:
This is more a matter of style than anything else, but I feel that it cleans the functions up a little and makes them easier to reuse.


Then it's far more than just "style". It's about proper design, having indepedent modules that have a single responsibility(only doing the conversion, not also printing the results) and can be used by any higher-level module(such as main). For example, another module may not want to print the results, but use them in subsequent calculations or displaying them in another fashion such with as with graphics. This is impossible with the OP's design. Of course, he probably doesn't want to do anything else with this little program but that's besides the point; he should learn how to properly do things, after all that's what these little exercises are all about.
Quote:Original post by mikeman
Quote:
This is more a matter of style than anything else, but I feel that it cleans the functions up a little and makes them easier to reuse.


Then it's far more than just "style". It's about proper design, having indepedent modules that have a single responsibility(only doing the conversion, not also printing the results) and can be used by any higher-level module(such as main). For example, another module may not want to print the results, but use them in subsequent calculations or displaying them in another fashion such with as with graphics. This is impossible with the OP's design.


Right. Poor choice of words on my part.
Quote:Original post by ZahlmanDigiDude: Your "correcion" to the arithmetic is quite incorrect.
My bad, you're right, doesn't need correcting. Seems the same though.
F-R-E-D F-R-E-D-B-U-R...G-E-R! - Yes!

This topic is closed to new replies.

Advertisement