Help Please!! Adding Kelvin conversion to program

My program converts temperature from F to C and vice versa. An option to also show the Kelvin units is also given and must be shown after the previous conversion. I'm having trouble with the function and function call for that.

This is a sample of how the program should run.


Welcome to the Temperature Converter!

Would you like to see degrees Kelvin (K) in the results? (Y/N): Y

Select conversion type (1=F to C, 2=C to F, 0=end): 1
Enter your Fahrenheit temperature: 32
A temp of 32 Fahrenheit converted to Celsius = 0 C.
This is also a temperature of: 273.15 K

Select conversion type (1=F to C, 2=C to F, 0=end): 2
Enter your Celsius temperature: 100
A temp of 100 Celsius converted to Fahrenheit = 212 F.
This is also a temperature of: 373.15 K

Select conversion type (1=F to C, 2=C to F, 0=end): 0

Thanks for using the temperature converter!


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

using namespace std;
using namespace System;

double FtoC(double t);
double CtoF(double t);
double showDegreesK(double j);

int main()
{
	double temp, result, kresult;
	int choice, kchoice;

	cout << "Welcome to the temp conversion program." << endl;

	do
	{
		cout << "Would you like to see degrees Kelvin (K) in the results? (Y/N): ";
		cin >> kchoice;
		cout << "\nConversion Type (1=F to C, 2=C to F, 0=Quit: ";
		cin >> choice;
		if (choice == 1)
		{
			cout << "Enter your Fahrenheit temp: ";
			cin >> temp;

			result = FtoC(temp);
			cout << "A temp of " << temp << "F is equal to " << result << "C." << endl;
			showDegreesK(kchoice);
		}

		else if (choice == 2)
		{
			cout << "Enter your Celcius temp: ";
			cin >> temp;

			result = CtoF(temp);
			cout << "A temp of " << temp << "C is equal to " << result << "F." << endl;
			showDegreesK(kchoice);
		}
	} while (choice != 0);

	cout << "\nThanks for using the temp converter!" << endl;
	system("Pause");
    return 0;
}

double FtoC(double t)
{
	double r;
	r = (5.0/9.0 * (t - 32.0));

	return r;
}

double CtoF(double t)
{
	double r;
	r = ((9.0 / 5.0) * t) + 32;

	return r;
}

double showDegreesK(double j, int choice, int kchoice, double result)
{
	double kresult;

	if (kchoice == 'Y' && choice == 1)
	{
		kresult = result + 273.15;
	}

	else if (kchoice == 'Y' && choice == 2)
	{
		kresult = (result + 459.67) * (5.0/9.0);
	}
	return kresult;
}
Last edited on
try making CtoK() and FtoK() functions.
or just CtoK()

1
2
3
if(kchoice == 'Y'){
   std::cout << CtoK(tempC) << '\n';
}
Last edited on
Bump. The showDegreesK() function is required and should only output the Kelvin temperature when the user enters 'Y'. I'm not that great with functions so if anyone can show me how it's done it'd be greatly appreciated.
I thought I responded to this yesterday.

At line 20 you prompt for Y/N but you're reading the result into kchoice, which is declared as an int at line 14. Change kchoice to be a char instead.

What should showDegreesK() do? It takes a choice, and a kchoice and some value j that isn't used and some parameter "result" that isn't used either. You'll find that code is easier to write if you're clear about what a function should do. A great way to do this is to add a comment to the function. That way you can be sure to call the function properly AND to write it so it does what it's supposed to do.

From what you've described, it showDegrees should print the degree value in Kelvin, but only if kchoice is 'Y' in the main program.

I see that in both places where you call showDegrees(), you know both the Fahrenheit and Celsius temperature. Since converting from Celsius is easier, let's pass in just that value.

With all this info, we can write the prototype for showDegrees:
1
2
// If kchoice is 'Y' then show "celsius" degrees in Kelvin
void showDegreesK(char kchoice, double celsius)


Write the code for showDegreesK() with the prototype I've given. Be sure to change line 9 to match.

Then change the two calls to showDegreesK at lines 31 and 41. Be sure to pass the temperature in Celsius and not Fahrenheit as the second parameter.

1
2
// If kchoice is 'Y' then show "celsius" degrees in Kelvin
void showDegreesK(char kchoice, double celsius)
I don't like that. `showDegreesK()' is doing two things:
- converting from celsius to kelvin
- parsing the option.
I would leave that second task to `main()'
ne555, that's a very good point. Something like:
in main:
1
2
3
4
char ch;
cout << "Would you like to see degrees Kelvin (K) in the results? (Y/N): ";
cin >> ch;
bool doKelvin = (ch == 'Y');

and then showDegreesK() becomes:
void showDegreesK(bool doKelvin, double celcius);

Topic archived. No new replies allowed.