Need help with a loop error.

Hi all. I'm very new at c++ and I just started writing a temperature conversion program:

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

//Converts Temperatures

double ctof(double c1) //converts Celcius to Fahrenheit
{
	double f = (c1 * 1.8) + 32;
	return f;
}

double ctok(double c1) //converts Celcius to Kelvin
{
	double kc = c1 + 273.15;
	return kc;
}
double ftoc(double f1) //converts Fahrenheit to Celsius
{
	double c = ((f1 - 32) * 5) / 9;
	return c;
}

double ftok(double f1) //converts Fahrenheit to Kelvin
{
	double kf = ((f1 - 32) * 5) / 9 + 273.15;
	return kf;
}

double ktof(double k1) //converts Kelvin to Fahrenheit
{
	double fk = ((k1 - 273.15) * 9) / 5 + 32;
	return fk;
}

double ktoc(double k1) //converts Kelvin to Celsius
{
	double ck = k1 - 273.15;
	return ck;
}

int main()
{	
	double c1 = 0;
	double f1 = 0;
	double k1 = 0;
	double num;

	cout << "Input a unit of temperature:\n(1 = fahrenheight, 2 = celsius, 3 = kelvin)\n";
	
	cin >> num;
	
	while (num != 1 || num != 2 || num != 3) {
		
		cout << "Invalid number. Input a unit of temperature:\n(1 = fahrenheight, 2 = celsius, 3 = kelvin)\n";
	
		cin >> num;
	}
		
	if (num == 1) {
		
		cout << "Please enter degree in Fahrenheit: ";
		cin >> f1; //retrieve Kelvin temperature to input variable
	
		double c = ftoc(f1); //convert temperature
		cout << c << " Celcius\n"; //print out temperature
	
		double k = ftok(f1);
		cout << k << " Kelvin\n";
	
	}
	
	else if (num == 2) {
		
		cout << "Please enter degree in Celcius: ";
		cin >> c1; //retrieve Clecius temperature to input variable
		while(c1 < -273.15){
			cerr << "Invalid input. Enter another number: ";
			cin >> c1; 
		}
		
		double f = ctof(c1); //convert temperature
		cout << f << " Fahrenheit\n"; //print out temperature
		
		double k = ctok(c1);
		cout << k << " Kelvin\n";
		
	}
	
	else if (num == 3) {
	
		cout << "Please enter degree in Kelvin: ";
		cin >> k1; //retrieve Kelvin temperature to input variable
		while(k1 < 0){
			cerr << "Invalid input. Enter another number: ";
			cin >> k1;
		}
		
		double f = ktof(k1);
		cout << f << " Fahrenheit\n";
		
		double c = ktoc(k1); //convert temperature
		cout << c << " Celcius\n"; //print out temperature
		
	}
}


In my main I am trying to make a loop to check that the user inputs a valid number for the unit of temperature; 1 for fahrenheit, 2 for celsius, or 3 for kelvin. If they don't, I want to ask the user for input again. When I do it though, it says it's an invalid number even when it isn't. Do I need to clear the buffer perhaps?...I'm sure it's a simple problem but I can't seem to figure it out. Any help would be much appreciated!
while (num != 1 || num != 2 || num != 3)

You want to use logical and, since that loop's condition will always be true. The only way that condition will be false is if num is equal to 1, 2, and 3, all at once.

while (num != 1 && num != 2 && num != 3)

This loop will quit once num is equal to either 1, 2, or 3.
Last edited on
I feel like such an idiot =P Talk about simple. Thanks a lot...I have another question though. Instead of having them input a number for the unit of temperature, how do I have them input something like f for fahrenheit, c for celsius, etc?
In c++ a char ("c","C","k","K","f","F") is literally a integer that represents a number.

so "C" (uppercase) is the number 67
and "c" (lowercase) is the number 99
"K" = 75 (Upper)
"k" = 107 (Lower)
"F" = 70 (Upper)
"f" = 102 (Lower)

That means
1
2
3
4
if ('k' == 107)
{
    cout << "It can be an int!";
}


would result in:
It can be an int!


All the values for the chars are written here: http://www.cplusplus.com/doc/ascii/
Last edited on
I also feel I should let you know that true and false are also numbers...

false ALWAYS equals 0

If it is not 0, then it is true!

so...
1
2
3
4
if ('0' > 0)
{
    cout << "'0' is less than zero!";
}

results in:
'0' is less than zero!

a null char is '\0' and that is because you are setting the character to the number value 0, not to the letter value zero... if that makes any sense to you :)

Either ither or, good luck programming and I hope I made life easier :)
Last edited on
that is why a string of numbers to an int is pretty complex...
in binary...
"0" = 00110000
"1" = 00110001
"2" = 00110010
"3" = 00110011
"4" = 00110100
"5" = 00110101
"6" = 00110110
"7" = 00110111
"8" = 00111000
"9" = 00111001

in binary...
0 = 00000000
1 = 00000001
2 = 00000010
3 = 00000011
4 = 00000100
5 = 00000101
6 = 00000110
7 = 00000111
8 = 00001000
9 = 00001001

so there is a direct link between them but then you have numbers like 47...
"48" = 00110100,00110111 (a '4' and a '7")
47 = 00101111

So that is why we have beautiful atoi : )
Topic archived. No new replies allowed.