sending/recieving from function

Hi there, I am writing a program that will convert F to C or C to F with the option of converting either to K. I am having trouble passing the F or C to my function for Kelvin; I am getting a 0 every time. I feel I am doing something wrong with my variables, but I am not sure. Could anyone push me in the right direction?

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  #include "stdafx.h"
#include<iostream>

using namespace std;
using namespace System;



double  answer;
double FtC(double t);
double CtF(double t);
double showDegreesK (double t);






int main()
{
	double answer = 0;
	char answerkelvin;
	int ans;
	double temp;


	cout << "Welcome to my temperature converter." << endl << endl;

	do
	{
		cout << "Would you like to see the temperature in Kelvin? y for yes, n for no" << endl;
		cin >> answerkelvin;

	

		cout << "Which conversion type would you like to do:" << endl;
		cout << "1. F to C \n2. C to F \n0. End Program" << endl;
		cin >> ans;


		if (ans == 1)
		{
			cout << "Please input your desired temperature in F to be converted:" << endl;
			cin >> temp;
			answer = FtC(temp);

		
			if (answerkelvin == 'y')
			{	
				
				showDegreesK(answer);
				
				if (answer<0)
				{
					cout << "That temperature is impossible" << endl;
				}
				
				else
				{
					cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
					cout << "The temperature in Kelvin is " << answer << endl;
				}
			}
			
			else 
			{
				
				cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
			}



		}

		else if (ans == 2)
		{
			cout << "Please input your desired temperature in C to be converted: " << endl;
			cin >> temp;
			answer = CtF(temp);
			
			if (answerkelvin == 'y')

			{
				
				showDegreesK(answer);

				if (answer < 0)
				{
					cout << "That temperature is impossible"<<endl;

				}
		
				else
				{
					cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
					cout << "The temperature in Kelvin is " << answer << endl;
				}
			}
			
			else 
			{
				cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			}

			
		}
		
		else
		{
			cout << "You entered an invalid key." << endl;
		}

	} while (ans != 0);

	cout << "Thank you for using my temperature converting program!" << endl;
	system("Pause");
	return 0;
}
double FtC(double t)
{
	double a;

	a = (5.0 / 9.0)*(t - 32.0);
	return a;
}
double CtF(double t)
{
	double a;

	a = ((9.0 / 5.0*t)) + 32;
	return a;
}

double showDegreesK(double t)
{

	double f, c;



	f = answer + 273.15;



	c = (answer + 459.67)*(5.0 / 9.0);



		
	return t;
	
} 
Remove the global on line 9. I think this is complicating the flow of your program and making it more difficult for you to reason about it.
Don't use any globals at all and give it another try, then come back if you're still having problems.
I know globals are considered 'bad'; I used them because I was having trouble getting it to compile otherwise. I guess my question is, how could I get around using globals? And my original question is how can I get my showDegreesK function to know whether it is receiving a degree in C or in F?
I got it to work (without the showDegreesK function) without the global, not to bad. But I am still stuck on converting C or F to Kelvin.
Something simple: Implement C->K, and then implement F->K using the functions you already have (F->K = F->C->K).
Could you put that in laymans terms? Is '->' an operator; if so what does it do?
No. It's just an ad hoc notation implying unit conversion.
oh ok. I am trying to have it to it only sends a temp in C to the function, where am I going wrong with my variables? I think I am calling my functions correctly.

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "stdafx.h"
#include<iostream>

using namespace std;
using namespace System;




double FtC(double t);
double CtF(double t);
double showDegreesK (double t);






int main()
{
	double answer = 0, temp;
	char answerkelvin;
	int ans;



	cout << "Welcome to my temperature converter." << endl << endl;

	do
	{
		cout << "Would you like to see the temperature in Kelvin? y for yes, n for no" << endl;
		cin >> answerkelvin;

	

		cout << "Which conversion type would you like to do:" << endl;
		cout << "1. F to C \n2. C to F \n0. End Program" << endl;
		cin >> ans;


		if (ans == 1)
		{
			cout << "Please input your desired temperature in F to be converted:" << endl;
			cin >> temp;
			answer = FtC(temp);

		
			if (answerkelvin == 'y')
			{	
				
				showDegreesK(temp);

				
				if (temp<0)
				{
					cout << "That temperature is impossible" << endl;
				}
				
				else
				{
					cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
					cout << "The temperature in Kelvin is " << answer << endl;
				}
			}
			
			else 
			{
				
				cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
			}



		}

		else if (ans == 2)
		{
			cout << "Please input your desired temperature in C to be converted: " << endl;
			cin >> temp;
			answer = CtF(temp);
			
			if (answerkelvin == 'y')

			{
				
				showDegreesK(temp);

				if (answer < 0)
				{
					cout << "That temperature is impossible"<<endl;

				}
		
				else
				{
					cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
					answer = FtC(temp);
					showDegreesK(temp);
					cout << "The temperature in Kelvin is " << answer << endl;
				}
			}
			
			else 
			{
				cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			}

			
		}
		
		else
		{
			cout << "You entered an invalid key." << endl;
		}

	} while (ans != 0);

	cout << "Thank you for using my temperature converting program!" << endl;
	system("Pause");
	return 0;
}
double FtC(double t)
{
	double a;

	a = (5.0 / 9.0)*(t - 32.0);
	return a;
}
double CtF(double t)
{
	double a;

	a = ((9.0 / 5.0*t)) + 32;
	return a;
}

double showDegreesK(double t)
{

	double answer = answer + 273.15;
		
	return answer;
	
} 
Line 140: answer doesn't have any particular value before you assign it. You need to add absolute zero to the variable that actually contains the temperature value.
Lines 51, 87, 97, and 98: Your functions are pure now, so if you don't assign their return value to anything, calling them doesn't do anything.
1
2
3
4
5
6
7
c = 20;
answer = CtF(c);
//answer = 68; c still equals 20
answer = FtC(c);
//answer = -6.666...; c still equals 20
showDegreesK(c);
//no change in program state 


Also, rename showDegreesK(). It's a bad name because it doesn't actually show anything.
I am supposed to have the function named that. Alright, I fixed all that. Now, my error catch for having a negative kelvin isn't working. Should it not be answer<0? Here is my updated code:

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "stdafx.h"
#include<iostream>

using namespace std;
using namespace System;




double FtC(double t);
double CtF(double t);
double showDegreesK (double t);






int main()
{
	double answer = 0, temp;
	char answerkelvin;
	int ans;



	cout << "Welcome to my temperature converter." << endl << endl;

	do
	{
		cout << "Would you like to see the temperature in Kelvin? y for yes, any other key for no" << endl;
		cin >> answerkelvin;

	

		cout << "Which conversion type would you like to do:" << endl;
		cout << "1. F to C \n2. C to F \n0. End Program" << endl;
		cin >> ans;


		if (ans == 1)
		{
			cout << "Please input your desired temperature in F to be converted:" << endl;
			cin >> temp;
			answer = FtC(temp);

		
			if (answerkelvin == 'y')
			{	
				
				answer = showDegreesK(temp);

				
				if (answer<0)
				{
					cout << "That temperature is impossible" << endl;
				}
				
				else
				{
					cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
					cout << "The temperature in Kelvin is " << answer << endl;
				}
			}
			
			else 
			{
				
				cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
			}



		}

		else if (ans == 2)
		{
			cout << "Please input your desired temperature in C to be converted: " << endl;
			cin >> temp;
			answer = CtF(temp);
			
			if (answerkelvin == 'y')

			{
				
				answer = showDegreesK(temp);

				if (answer < 0)
				{
					cout << "That temperature is impossible"<<endl;

				}
		
				else
				{
					cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
					answer = FtC(temp);
					answer = showDegreesK(temp);
					cout << "The temperature in Kelvin is " << answer << endl;
				}
			}
			
			else 
			{
				cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			}

			
		}
		
		else if (ans == 0)
		{
			cout << "Thank you for using my temperature converting program!" << endl;

		}

		else
			cout << "You entered an invalid key" << endl;

	} while (ans != 0);

	
	system("Pause");
	return 0;
}
double FtC(double t)
{
	double a;

	a = (5.0 / 9.0)*(t - 32.0);
	return a;
}
double CtF(double t)
{
	double a;

	a = ((9.0 / 5.0*t)) + 32;
	return a;
}

double showDegreesK(double t)
{

	double answer = answer + 273.15;
		
	return answer;
	
} 
neverming, still not working. Still trying, don't understand it yet
ok, so i think i am ALMOST there. How would you go about putting in the error catch for negative kelvin, without outputting anything else but an error message? I am having trouble with the order of calling functions/cout to the screen.

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "stdafx.h"
#include<iostream>

using namespace std;
using namespace System;




double FtC(double t);
double CtF(double t);
double showDegreesK (double t);






int main()
{
	double answer = 0, temp,answer1;
	char answerkelvin;
	int ans;



	cout << "Welcome to my temperature converter." << endl << endl;

	do
	{
		cout << "Would you like to see the temperature in Kelvin? y for yes, any other key for no" << endl;
		cin >> answerkelvin;

	

		cout << "Which conversion type would you like to do:" << endl;
		cout << "1. F to C \n2. C to F \n0. End Program" << endl;
		cin >> ans;


		if (ans == 1)
		{
			cout << "Please input your desired temperature in F to be converted:" << endl;
			cin >> temp;

			if (answerkelvin = 'y')
			{
				answer = FtC(temp);
				cout << "The temperature " << temp << " in F is " << answer << endl;
				answer = showDegreesK(answer);
			
				cout << "The temperature in Kelvin is " << answer<<endl;
				
				
			}
			
			else
			{
				cout << "The temperature " << temp << "in F is " << answer << endl;
			}
		}

		else if (ans == 2)
		{
			cout << "Please input your desired temperature in C to be converted: " << endl;
			cin >> temp;
			
			if (answerkelvin = 'y')

			{
				answer = CtF(temp);
				cout << "The temperature " << temp << " in C is " << answer <<"in F"<<endl;
				answer = FtC(temp);
				answer = showDegreesK(answer);
				cout << "The temperature in Kelvin is " << answer<<endl;
			}

			else
			{
				cout << "The temperature " << temp << "in C is " << answer <<"in F"<<endl;
			}

		}
		else if (ans == 0)
		{
			cout << "Thank you for using my temperature converting program!" << endl;

		}

		else
			cout << "You entered an invalid key" << endl;

	} while (ans != 0);

	
	system("Pause");
	return 0;
}
double FtC(double t)
{
	double a;

	a = (5.0 / 9.0)*(t - 32.0);
	return a;
}
double CtF(double t)
{
	double a;

	a = ((9.0 / 5.0*t)) + 32;
	return a;
}

double showDegreesK(double t)
{

	double a = t + 273.15;
	if (a < 0)
		cout << "That temperature is impossible" << endl;

		
	return a;
	
} 

Topic archived. No new replies allowed.