Temperature Conversion

Hi. I am trying to program a temperature conversion program for a school project. It needs to accept and convert between Farenheit, Kelvin, Rankine, and Celsius. I am only half done with it, but as I'm testing what I have I can't get the last part to work correctly; it's the part with the else statements. In the version I've posted here, the program outputs the conversion from K to F regardless of what is input for d_unit (desired unit).

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
//Temperature Conversion
//Stephan Pichardo - 10/10/14

#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
	char unit, d_unit;
	double value;

	double C = 0;
	double K = 0;
	double F = 0;
	double R = 0;

//prompt for initial inputs

cout << "Please input value to be converted.\n";
cin >> value;
cout << "What are the units of that value?\n";
cin >> unit;

//case check

	if (unit > 90)
		unit -= 32;

//conversion from K

	{ if (unit == K)

//calc from K

	C = value - 273.15;
	F = value * ( 9.0 / 5.0 ) - 459.67;
	R = value * ( 9.0 / 5.0 );
	K = value;
	
//prompt for desired unit

cout << "What unit would you like to convert to?\n";
cin >> d_unit;

//case check
	if (d_unit > 90)
		d_unit -= 32;

//output desired unit

	if (d_unit == C)
	{
cout << "" << value << " degrees Kelvin converts to " << C << " degrees Celcius.\n";
	}
	else if (d_unit == K)
	{
cout << "" << value << " degrees Kelvin converts to " << K << " degrees Kelvin.\n";
	}
	else if (d_unit == R)
	{
cout << "" << value << " degrees Kelvin converts to " << R << " Rankines.\n";
	}
	else (d_unit == F);
	{
cout << "" << value << " degrees Kelvin converts to " << F << " degrees Farenheit.\n";
	}
	}
	return 0;
}
The problem is the trailing end of line character left by the numeric entry. You'll need to either ignore or skip this whitespace character.

1
2
3
4
5
6
7
8
9
10
// Ignore that one end of line character.
cin >> value;
cin.ignore();
cin >> unit;


// Skip the leading whitespace.
cin >> value; 

cin >> skipws >> unit >> noskipws; 


With the second method be sure to remember the noskipws, to restore "normal" operation since this "flag" stays in force until you change it back.

Last edited on
Thanks for your reply. Unfortunately, I'm not sure I understand. Whatever the case I tried to start again with a simpler program, and I still need help.

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
//Temperature Conversion
//Stephan Pichardo - 10/10/14

#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
	char unit, d_unit;
	double value;
    double C = 0;
	double K = 0;
	double F = 0;
	double R = 0;

//math

	C = K -273.15;
	F = K * (9.0 / 5.0) - 459.67;
	R = K * (9.0 / 5.0);
	K = K;
	K = (F + 459.67) * (9.0 / 5.0);
	K = C + 273.15;
	K = R * (9.0 / 5.0);

//prompts and inputs

cout << "Input the value to be converted: ";
cin >> value;
cout << "\nWhat are the units for that value? ";
cin >> unit;
cout << "\nWhat units is the value being converted to? ";
cin >> d_unit;

//if unit == K

	if (unit == K)
	{
		K = value;

		if (d_unit == R)

			cout << "/n/nThat value converts to " << R << " degrees Rankene.";
	}

	return 0;

}


Again, I'm partially finished so I was trying to test what happens when I input K for unit and R for d_unit, because I have coded those conditions. The program just ends when I input R for d_unit; why is it ignoring the if statement?
What are you inputting to your three variables?

Do you realize that K is the name of a variable, not the letter 'K'?

I guess I really don't know what I'm doing. I want K (Kelvin) to be the unit we convert from (in this case) into R (Rankene).
The problem is that unit is equal to 'K', not the value held in K. Perhaps you need to study this tutorial:
http://www.cplusplus.com/doc/tutorial/variables/
Ok, I got it. I only need one more thing: I must be misunderstanding the info on loops, because I can't this thing to loop like I want. At the end of a conversion I want to prompt for if the user wants to do another conversion by having them enter y or n. If n, the program ends and if y it repeats. Can someone walk me through it? I swear I tried everything to the best of my understanding but nothing will take...

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

//Temperature Conversion
//Stephan Pichardo - 10/10/14

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main ()
{

	char unit, d_unit;
	double value;
    double C = 0;
	double K = 0;
	double F = 0;
	double R = 0;

cout << "This program converts between Celsius, Kelvin, Rankene, \nand Farenheit temperatures.\n";

cout << "\nInput the value to be converted: ";
cin >> value;
cout << "\nWhat are the units for that value? (K/R/C/F) ";
cin >> unit;

	switch(unit)
	{
	case 'K': case 'k':
		cout << "\nWhat unit would you like to convert to? ";
		cin >> d_unit;

			C = value -273.15;
			F = value * (9.0 / 5.0) - 459.67;
			R = value * (9.0 / 5.0);
			K = value;
	
		switch(d_unit)
		{
		case 'R': case 'r':
			cout << "\n" << value << " degrees Kelvin converts to " << R << " degrees Rankene.";
			break;
		case 'F': case 'f':
			cout << "\n" << value << " degrees Kelvin converts to " << F << " degrees Farenheit.";
			break;
		case 'K': case 'k':
			cout << "\n" << value << " degrees Kelvin converts to " << K << " degrees Kelvin.";
			break;
		case 'C': case 'c':
			cout << "\n" << value << " degrees Kelvin converts to " << C << " degrees Celsius.";
			break;
		}
		break;
	case 'F': case 'f':
		cout << "\nWhat unit would you like to convert to? ";
		cin >> d_unit;

			K = (value + 459.67) * (5.0 / 9.0);
			C = K - 273.15;
			R = K * (9.0 / 5.0);
			F = value;
	
		switch(d_unit)
		{
		case 'R': case 'r':
			cout << "\n" << value << " degrees Farenheit converts to " << R << " degrees Rankene.";
			break;
		case 'F': case 'f':
			cout << "\n" << value << " degrees Farenheit converts to " << F << " degrees Farenheit.";
			break;
		case 'K': case 'k':
			cout << "\n" << value << " degrees Farenheit converts to " << K << " degrees Kelvin.";
			break;
		case 'C': case 'c':
			cout << "\n" << value << " degrees Farenheit converts to " << C << " degrees Celsius.";
			break;
		}
		break;
	case 'R': case 'r':
		cout << "\nWhat unit would you like to convert to? ";
		cin >> d_unit;

			K = value * (5.0 / 9.0);
			C = K - 273.15;
			R = value;
			F = K * (9.0 / 5.0) - 459.67;
	
		switch(d_unit)
		{
		case 'R': case 'r':
			cout << "\n" << value << " degrees Rankene converts to " << R << " degrees Rankene.";
			break;
		case 'F': case 'f':
			cout << "\n" << value << " degrees Rankene converts to " << F << " degrees Farenheit.";
			break;
		case 'K': case 'k':
			cout << "\n" << value << " degrees Rankene converts to " << K << " degrees Kelvin.";
			break;
		case 'C': case 'c':
			cout << "\n" << value << " degrees Rankene converts to " << C << " degrees Celsius.";
			break;
		}
		break;
	case 'C': case 'c':
		cout << "\nWhat unit would you like to convert to? ";
		cin >> d_unit;

			K = value + 273.15;
			F = K * (9.0 / 5.0) - 459.67;
			R = K * (9.0 / 5.0);
			C = value;
	
		switch(d_unit)
		{
		case 'R': case 'r':
			cout << "\n" << value << " degrees Farenheit converts to " << R << " degrees Rankene.";
			break;
		case 'F': case 'f':
			cout << "\n" << value << " degrees Farenheit converts to " << F << " degrees Farenheit.";
			break;
		case 'K': case 'k':
			cout << "\n" << value << " degrees Farenheit converts to " << K << " degrees Kelvin.";
			break;
		case 'C': case 'c':
			cout << "\n" << value << " degrees Farenheit converts to " << C << " degrees Celsius.";
			break;
		}
		break;
	}

cout << "\n\n";
	return 0;
}


What loops are you talking about, I don't see any in your code?

I do however see a lot of code duplication that you should try to eliminate.
Topic archived. No new replies allowed.