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()
{
menu:
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";
cout<<"Choose your option: ";
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':
goto menu;

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':
goto menu;

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':
goto menu;

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

break;
}
return 0;
}
Last edited on
integer math. 9/5 is 1
5/9 is zero
just add a decimal:
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.

1
2
3
4
5
6
7
8
9
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<<fixed<<setprecision(2); //this does not belong here. it belongs near where you print. 
    double c, f; //what is C??
    return 1.8 * degf +32; //?? what is C?
    f = 9/5 * c + 32;
    return f;
}


or cleaned up fully:
1
2
3
4
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)
Show your current code.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

double CtoF (double cs, double fr)
{
    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()
{
    menu: 
    double c, k, f;
    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;
            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':
                    goto menu;
                    
                case 'N' : case 'n':
                    return 0;
            }
            
            break;
            
        case 2:
            cout<<"Enter temperature in Fahrenheit: ";
            cin>>f;
            FtoC (f, c);
            cout<<f <<" degree Fahrenheit is " <<c <<" degree Celsius. \n \n";
            cout<<"Do you want to continue [Y/N]? ";
            cin>>cont;
            cout<<endl;
            
            switch (cont)
            {
                case 'Y' : case 'y':
                    goto menu;
                    
                case 'N' : case 'n':
                    return 0;
            }
            
            break;
            
        case 3:
            cout<<"Enter temperature in Celsius: ";
            cin>>c;
            CtoK (c, k);
            cout<<c <<" degree Celsius is " <<k <<" Kelvin. \n \n";
            cout<<"Do you want to continue [Y/N]?" ;
            cin>>cont;
            cout<<endl;
        
            switch (cont)
            {
                case 'Y' : case 'y':
                    goto menu;
                    
                case 'N' : case 'n':
                    return 0;
            }
            
            break;    
    }
    return 0;
}
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <cmath>
#include <iomanip>
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<<fixed<<setprecision(2);
            cout<<c <<" degree Celsius is " <<fahrenheit <<" degree Fahrenheit. \n \n";
            cout<<"Do you want to continue [Y/N]? ";
            cin>>cont;
            cout<<endl;
            
            switch (cont)
            {
                case 'Y' : case 'y':
                    goto menu;
                    
                case 'N' : case 'n':
                    return 0;
            }
            
            break;
            
        case 2:
            cout<<"Enter temperature in Fahrenheit: ";
            cin>>f;
            celsius = FtoC (celsius);
            cout<<fixed<<setprecision(2);
            cout<<f <<" degree Fahrenheit is " <<celsius <<" degree Celsius. \n \n";
            cout<<"Do you want to continue [Y/N]? ";
            cin>>cont;
            cout<<endl;
            
            switch (cont)
            {
                case 'Y' : case 'y':
                    goto menu;
                    
                case 'N' : case 'n':
                    return 0;
            }
            
            break;
            
        case 3:
            cout<<"Enter temperature in Celsius: ";
            cin>>c;
            kelvin = CtoK (kelvin);
            cout<<fixed<<setprecision(2);
            cout<<c <<" degree Celsius is " <<kelvin <<" Kelvin. \n \n";
            cout<<"Do you want to continue [Y/N]?" ;
            cin>>cont;
            cout<<endl;
        
            switch (cont)
            {
                case 'Y' : case 'y':
                    goto menu;
                    
                case 'N' : case 'n':
                    return 0;
            }
            
            break;    
    }
}
loner123 wrote:
Ive applied what jonnin said

No - you have completely and deliberately ignored it.


Take your code:
1
2
3
4
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
Your maths is adrift @seeplus.
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cmath>
#include <iomanip>
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.
Registered users can post here. Sign in or register to post.