### trying to understand functions

Can someone please tell me what I did wrong? my program won't solve for the answers. It's my first program using functions.

Here's the problem with the program:

for celsius to fahrenheit, the output of celsius is displayed but the output for fahrenheit is always 0.00 (ex. 25.5 C is 0.00 F)

for fahrenheit to celsius, the output of fahrenheit is displayed but the output for celsius is the value that was used for the previous conversion (ex. 53.6 F is 25.5 C)

for celsius to kelvin, the output of celsius is displayed but the output for kelvin is always 0.00 (ex. 1 C is 0.00 K)

#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

double CtoF (double a, double b)
{
cout<<fixed<<setprecision(2);
double c, f;
f = 9/5 * c + 32;
return f;
}

double FtoC (double fahr, double cel)
{
cout<<fixed<<setprecision(2);
double c, f;
f = (f - 32) * 5/9;
return c;
}

double CtoK (double C, double K)
{
cout<<fixed<<setprecision(2);
double c, k;
k = c + 273.15;
return k;
}

int main()
{
double c, k, f, ans;
int opt;
char cont;

cout<<"******************************************************************************************************************\n";
cout<<" TEMPERATURE CONVERTER\n";
cout<<"******************************************************************************************************************\n";
cout<<" [1] - Celsius to Fahrenheit\n";
cout<<" [2] - Fahrenheit to Celsius\n";
cout<<" [3] - Celsius to Kelvin\n";
cout<<"******************************************************************************************************************\n";
cin>>opt;

switch (opt)
{
case 1:
cout<<"Enter temperature in Celsius :";
cin>>c;
CtoF (c, f);
cout<<c <<" degree Celsius is " <<f <<" degree Fahrenheit. \n \n";
cout<<"Do you want to continue [Y/N]? ";
cin>>cont;
cout<<endl;

switch (cont)
{
case 'Y' : case 'y':

case 'N' : case 'n':
return 0;
}

break;

case 2:
cout<<"Enter temperature in Fahrenheit :";
cin>>f;
FtoC (f, c);
cout<<c <<" degree Fahrenheit is " <<f <<" degree Celsius. \n \n";
cout<<"Do you want to continue [Y/N]? ";
cin>>cont;
cout<<endl;

switch (cont)
{
case 'Y' : case 'y':

case 'N' : case 'n':
return 0;
}

break;

case 3:
cout<<"Enter temperature in Celsius :";
cin>>c;
CtoK (c, k);
cout<<c <<" degree Celsius is " <<f <<" Kelvin. \n \n";
cout<<"Do you want to continue [Y/N]?" ;
cin>>cont;
cout<<endl;

switch (cont)
{
case 'Y' : case 'y':

case 'N' : case 'n':
return 0;
}

break;
}
return 0;
}
Last edited on
integer math. 9/5 is 1
5/9 is zero
9/5.0
5/9.0
etc

also please use code tags. <> on the editor or code & /code in [] brackets
its <cmath> not math.h (math.h is for C programs)

c is not initialized in at least some of your functions.

useless variables are useless. harmless, but clutter etc.

 ``123456789`` ``````consider double CtoF (double degf); //a, double b) //what are a and b, and why not use them somewhere? and no ; on this line!!! { cout<

or cleaned up fully:
 ``1234`` ``````double CtoF (double degc) { return 1.8 * degc +32; }``````
Last edited on
hello, i tried fixing it but still no luck...

Here's the problem with the program:

for celsius to fahrenheit, the output of celsius is displayed but the output for fahrenheit is always 0.00 (ex. 25.5 C is 0.00 F)

for fahrenheit to celsius, the output of fahrenheit is displayed but the output for celsius is the value that was used for the previous conversion (ex. 53.6 F is 25.5 C)

for celsius to kelvin, the output of celsius is displayed but the output for kelvin is always 0.00 (ex. 1 C is 0.00 K)

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110`` ``````#include #include #include using namespace std; double CtoF (double cs, double fr) { cout<>opt; switch (opt) { case 1: cout<<"Enter temperature in Celsius: "; cin>>c; CtoF (c, f); cout<>cont; cout<>f; FtoC (f, c); cout<>cont; cout<>c; CtoK (c, k); cout<>cont; cout<
You "tried fixing it", but practically nothing has changed. Like you completely ignored what jonnin wrote.

If a function returns a value, then you have to store or use that returned value when you call the function.
See http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
My apologies.... i'll be sure to do my best to communicate when using this platform. I hope you'll still be willing to teach me.

Ive applied what jonnin said to my ability but I still can't seem to fix it. I guess its the initialization and calling of the function part?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103`` ``````#include #include #include using namespace std; double CtoF (double degc) { return 5/9 * degc + 32; } double FtoC (double degf) { return degf - 32 * 9/5; } double CtoK (double cel) { return cel + 273.15; } int main() { menu: double c, k, f, celsius, fahrenheit, kelvin; int opt; char cont; cout<<"******************************************************************************************************************\n"; cout<<" TEMPERATURE CONVERTER\n"; cout<<"******************************************************************************************************************\n"; cout<<" [1] - Celsius to Fahrenheit\n"; cout<<" [2] - Fahrenheit to Celsius\n"; cout<<" [3] - Celsius to Kelvin\n"; cout<<"******************************************************************************************************************\n"; cout<<"Choose your option: "; cin>>opt; switch (opt) { case 1: cout<<"Enter temperature in Celsius: "; cin>>c; fahrenheit = CtoF (fahrenheit); cout<>cont; cout<>f; celsius = FtoC (celsius); cout<>cont; cout<>c; kelvin = CtoK (kelvin); cout<>cont; cout<
loner123 wrote:
Ive applied what jonnin said

No - you have completely and deliberately ignored it.

 ``1234`` ``````double CtoF (double degc) { return 5/9 * degc + 32; }``````

Firstly, your conversion formula is wrong anyway.

Secondly, but more repeatedly in your coding, you are ignoring integer division. The first mathematical operation done in that line is
5 / 9
Since 5 and 9 are both int constants, the result is an int constant. The result will be truncated to the next integer below for positive results, so it will yield 0. If you wrote the ostensibly more correct 9/5 it would still truncate down to 1.

Either just add a decimal point to one of the numbers (e.g. 5.0 instead of 5) or, once you have written the fraction the correct way up, use the end result 1.8.

I am well aware that you get away with this in Python 3, but C++, in common with most other languages, does integer division when the operands are integers.

The formulas used are wrong.

L8 replace with
 `` `` ``return 9.0/5 * degc + 32.0;``

and L13
 `` `` ``return (degf - 32.0) * 5.0/9;``

5/9 and 9/5 use integer arithmetic - so the result is an integer (0 and 1 respectively). To get the division to return a real result, one (or both) of the dividend and divisor needs to be of type real. Hence using 9.0 instead of 9. 9.0 is a real number so the result is real.
Last edited on
My edited post crossed with yours.
The lines
` fahrenheit = CtoF (fahrenheit);`
and
` celsius = FtoC (celsius);`
don't make much sense either.
@Op you are calling the functions incorrectly. Also don't use goto. It's considered bad practice. Consider:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include #include #include using namespace std; double CtoF(double degc) { return 9.0 / 5 * degc + 32.0; } double FtoC(double degf) { return (degf - 32.0) * 5.0 / 9; } double CtoK(double cel) { return cel + 273.15; } int main() { double t {}; for (int opt{}; opt != 4; ) { cout << "******************************************************************************************************************\n"; cout << " TEMPERATURE CONVERTER\n"; cout << "******************************************************************************************************************\n"; cout << " [1] - Celsius to Fahrenheit\n"; cout << " [2] - Fahrenheit to Celsius\n"; cout << " [3] - Celsius to Kelvin\n"; cout << " [4] - Quit\n"; cout << "******************************************************************************************************************\n"; cout << "Choose your option: "; cin >> opt; switch (opt) { case 1: cout << "Enter temperature in Celsius: "; cin >> t; cout << fixed << setprecision(2); cout << t << " degree Celsius is " << CtoF(t) << " degree Fahrenheit.\n"; break; case 2: cout << "Enter temperature in Fahrenheit: "; cin >> t; cout << fixed << setprecision(2); cout << t << " degree Fahrenheit is " << FtoC(t) << " degree Celsius. \n \n"; break; case 3: cout << "Enter temperature in Celsius: "; cin >> t; cout << fixed << setprecision(2); cout << t << " degree Celsius is " << CtoK(t) << " Kelvin. \n \n"; break; case 4: break; default: cout << "Invalid option\n"; break; } } }``````

Last edited on
Thank you and again I apologize.