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).
//Temperature Conversion
//Stephan Pichardo - 10/10/14
#include <iostream>
#include <cmath>
usingnamespace 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";
}
elseif (d_unit == K)
{
cout << "" << value << " degrees Kelvin converts to " << K << " degrees Kelvin.\n";
}
elseif (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.
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.
//Temperature Conversion
//Stephan Pichardo - 10/10/14
#include <iostream>
#include <cmath>
usingnamespace 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?
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...