Calling a function for temperature conversion

I am having trouble correctly calling/creating my function for Kelvin.

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
  #include "stdafx.h"
#include<iostream>

using namespace std;
using namespace System;


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


int main()
{
	int ans;
	double temp, answer,answer1;
	char y;
	char ansk;


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

	do
	{

		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;

		cout << "Would you also like to convert the temperature to Kelvin? y for yes, anything else for no." << endl;
		cin >> ansk;

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

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

		
				
			
		}

		else
		{
			cout << "Please input your desired temperature in C to be converted: " << endl;
			cin >> temp;
			answer1 = CtF(temp);
			cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			cout << "The temperature in Kelvin is " << answer << 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 k)
{
	int ans;
	double temp1;
	

	if (ans == 1)
		k = (temp1 + 459.67)*(5.0 / 9.0);
	else
		k = (temp1)+273.15;

	return k;
}
Do I need to pass two variables though my showDegreesK function?
closed account (48T7M4Gy)
You would still need two variables ( ie temperature and an absolute flag) but it might be better to scrap the showDegrees function and incorporate the absolute decision in the two separate temperature conversion functions.

Also, it would be better to only have single cin lines in main for these two instead of 4 lines. There is no need for duplication.
What do you mean by absolute flag? Here is a slightly updated version, but I still can't pass the temp in C or F to my Kelvin function. (I have to use that name/function for a class). Also, my do while loop isn't working right. I can't get out of it when i want to.

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
#include "stdafx.h"
#include<iostream>

using namespace std;
using namespace System;


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

int main()
{
	int ans;
	double temp, answer;
	char y;
	


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

	do
	{

		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;
		cout << "Would you like to see the temperature in Kelvin?" << endl;
		cin >> ansk;

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

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



		}

		else
		{
			cout << "Please input your desired temperature in C to be converted: " << endl;
			cin >> temp;
			answer = CtF(temp);
			cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			if (ansk == 'y')

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

			else
			{
				cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << 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)
{
	char y;
	char ansk;
	double a;

	if (ansk == 1)
		a = t + 273.15;
	else
		a = (t+459.67)*(5.0/9.0);
	
	return a;

}
closed account (48T7M4Gy)
'absolute flag' just means your variable ansk. ie If the flag is up (say == 1) then calculate absolute, down (say == 0) normal or whatever.

Also, and I haven't had a close look yet, why is there a 5/9 factor in line 104?

Don't forget, you can pass two (or more) variables in a function.

eg double showDegreesK( double t, char a)

Last edited on
Ok, thank you i fixed it i think. I am having trouble with one more thing though; I am trying to put something in the showDegreesK function that will 'catch' any wrong temperatures since Kelvin can't be negative. How would I go about doing it inside that function, and sending back a message saying something along the lines of "You can't have negative Kelvin" or whatever.

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
#include "stdafx.h"
#include<iostream>

using namespace std;
using namespace System;


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

int main()
{
	int ans;
	double temp, answer;
	


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


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

			cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
			if (ansk == 'y')
			{
				answer = showDegreesK(temp);
				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);
			cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			if (ansk == 'y')

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

	} 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)
{
	char ansk;
	double a;

	if (ansk == 1)
		a = t + 273.15;
	else
		a = (t+459.67)*(5.0/9.0);
	
	return a;

}
Nevermind, not finished yet. Still not getting how to implement the showDegreesK function correctly. I shall try two variables and return, but is that necessary?
closed account (48T7M4Gy)
Yep, two variables are necessary whichever way you do it because the program (and user) has to decide which base temperature to use. It doesn't happen magically. :)

The test for negative temperatures could be something like

1
2
3
4
5
if(temp < 0)
  cout << "oops!";
  // ... some code to start again
else
 // code to continue on ... 

Ok, yea i am trying to use pass by reference but am getting stuck on how to get my two variables to my main. Do i have to create two new ones for my main, or just one, or just rename something? I am still fiddling with it for now.

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
#include "stdafx.h"
#include<iostream>

using namespace std;
using namespace System;




double FtC(double t);
double CtF(double t);
void showDegreesK(double& t, double& s);
char ansk;

int main()
{

	double temp, answer;
	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 >> ansk;


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

			cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
			if (ansk == 'y')
			{
				showDegreesK(temp);
				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);
			cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			if (ansk == 'y')

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

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

void showDegreesK(double& t, double& s)
{
	char ansk;
	double a;
	double b;

	if (ansk == 1)
		a = t + 273.15;
	else  
		b = (t + 459.67)*(5.0 / 9.0);
	
	if (a||b<0)
		cout << "That temperature is currently impossible" << endl;
	
	void;

}
closed account (48T7M4Gy)
You've boxed yourself in a bit.

The correct terminology is absolute temperature not K (for Kelvin) which just applies to C (for Celsius) temperatures.

So you could have a third variable to show that the starting temp is degrees C or degrees F and then pass it along to showDegreesK (double t, double& s, char scale) where scale is 'C' or 'F'.

But you can save all the trouble by FtC and CtF having two variables (temperature and flag) and do the calculation there.

1
2
3
4
5
6
7
8
9
double FtC(double t, double ansk)
{
	double a;

	a = (5.0 / 9.0)*(t - 32.0);
        if (ansk == 1)
            a = a + 273.15; // or a += 273.15
	return a;
}


BTW your line 115 won't work. should be if (a < 0 || b < 0) etc

:)
wow, thanks! I will probably be back but let me implement this real quick
closed account (48T7M4Gy)
OK - take your time
I tried using 3 variables in my showDegreesK function because I think we are supposed to only have one variable in our FtC and CtF functions. I am getting odd numbers with e to a huge power.

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

#include "stdafx.h"
#include<iostream>

using namespace std;
using namespace System;




double FtC(double t);
double CtF(double t);
void showDegreesK(double& temp, double& temp1, double& temp2);
char ansk;

int main()
{

	double temp, answer,temp1,temp2;
	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 >> ansk;


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

			cout << "\nA temperature of " << temp << " in F is " << answer << " in C." << endl;
			if (ansk == 'y')
			{
				showDegreesK(temp,temp1,temp2);
				cout << "The temperature in Kelvin is " << temp1 << 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);
			cout << "\nA temperature of " << temp << " in C is " << answer << " in F " << endl;
			if (ansk == 'y')

			{
				showDegreesK(temp,temp1,temp2);
				cout << "The temperature in Kelvin is " << temp2 << endl;
			}

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


		}
		else
		{
			
			break;
		}

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

void showDegreesK(double& t, double& a, double& b)
{
	char ansk;
	

	if (ansk == 1)
		 a = t + 273.15;
	else  
		b = (t + 459.67)*(5.0 / 9.0);
	
	if (a<0||b<0)
		cout << "That temperature is currently impossible" << endl;
	

}

closed account (48T7M4Gy)
I think we are supposed to only have one variable in our FtC and CtF functions


OK so have a ShowDegreesC and a ShowDegreesF function which you run after FtC or CtF

1
2
3
4
5
6
7
double ShowDegreesF(double temp, double ansk)
{
      double a;
        if (ansk == 1)
            a = a + 459.67; // or a += 459.67
	return a;
}
Last edited on
Yea, that is what i had originally but i think I am supposed to calculate Kelvin from C and F in one function named ShowDegreesK. That is what I am having a hard time with really. The other problems are minor that I am slowly learning right now. How would I pass one argument to ShowDegreesK, and pass 2 back?
closed account (48T7M4Gy)
It is customary to have one thread for one problem Jon, not two as you've done here and in the other thread you started. There is a lot of duplication going on. Good luck with your quest. :)
Last edited on
I didn't realize there were two threads for this same problem, that would explain a few things.
Thank you and my bad
closed account (48T7M4Gy)
No problem Jon, it's not the end of the world.
Topic archived. No new replies allowed.