#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";
}
elseif (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";
}
elseif (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!
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.
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?
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 :)
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