**** It now works the way I want it to, thank you everyone!! :) :)
*** Thank you! The Celsius works now, but why does the Kelvin still become 0?
** Thank you! I seem to have fixed the problem of the program ending after entering "kelvin" or "celsius". I also changed the spelling of Fahrenheit in my code. Now, the problem is that the answer I get is always 0, instead of the conversion...
// This program converts from Farenheight to Celsius or Kelvin
#include <iostream>
#include <string>
using namespace std;
int main() {
string input, Celsius, Kelvin;
double farenheight, celsius, kelvin;
cout <<"Hi! What is the weather today in Farenheight?? "<< endl;
cin >>farenheight;
cout <<"Would you like to convert this temperature to Celsius or Kelvin?"<<endl;
cin >> input;
if(input == Celsius)
{
cout << "Today's weather in Celsius is " <<celsius << " degrees Celsius! " << endl;
celsius = (5*(farenheight - 32)) / 9 ;
}
else if(input == Kelvin)
{
cout << "Today's weather in Kelvin is "<<kelvin <<" degrees Kelvin! " <<endl;
kelvin = (farenheight + 459.67)*(5/9);
}
}
// This program converts from Fahrenheit to Celsius or Kelvin
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
double Fahrenheit, celsius, kelvin;
cout << "Hi! What is the weather today in Fahrenheit?? "<< endl;
cin >> Fahrenheit;
cout << "Would you like to convert this temperature to Celsius or Kelvin?"<< endl;
cin >> input;
if(input == "Celsius")
{
celsius = (5*(Fahrenheit - 32)) / 9 ;
cout << "Today's weather in Celsius is " <<celsius << " degrees! " << endl;
}
else if(input == "Kelvin")
{
kelvin = (5*(Fahrenheit + 459.67)) / 9 ;
cout << "Today's weather in Kelvin is "<<kelvin <<" degrees!" <<endl;
}
return 0;
}
I'm trying to make it so it asks for an input of how many degrees in Fahrenheit you want, then asks whether you want to convert it to Celsius or Kelvin, and then proceeds to follow the equation and give the conversion. However, it seems to end after I type in I want Kelvin or Celsius — even if I just use the equation, the answer always comes out as 0...
Output is:
Hi! What is the weather today in Farenheight??
60
Would you like to convert this temperature to Celsius or Kelvin?
Celsius
Exit code: 0 (normal program termination)
I have corrected (using that word loosely here) the code that you've shared. Like others mentioned in the comments, your variable names are misspelled and were not initialized with a value.
Another mistake was that you were dividing 5 by 9 using integer division (farenheight + 459.67)*(5 / 9);. This causes type coercion, since both operands are of type int the result is coerced to an int, dropping any numbers after the decimal place. In this case 5 / 9 results in 0, so your entire result is multiplied by 0.
Another issue that would cause the results to be incorrect is that you are outputting the variables to the stream before you are declaring what the variables should contain. If you do this, whatever was stored in memory, the null string, will be output to the screen.
Here is the corrected code:
// This program converts from Farenheight to Celsius or Kelvin
#include <iostream>
#include <string>
using namespace std;
int main() {
string input, Celsius, Kelvin;
double farenheight = 0.0, celsius = 0.0, kelvin = 0.0;
cout << "Hi! What is the weather today in Farenheight?? " << endl;
cin >> farenheight;
cout << "Would you like to convert this temperature to Celsius or Kelvin?" << endl;
cin >> input;
if (input == "Celsius")
{
celsius = (5.0 * (farenheight - 32.0)) / 9.0;
cout << "Today's weather in Celsius is " << celsius << " degrees Celsius! " << endl;
}
else if (input == "Kelvin")
{
kelvin = (farenheight + 459.67)*(5.0 / 9.0);
cout << "Today's weather in Kelvin is " << kelvin << " degrees Kelvin! " << endl;
}
}
Two examples of output are here:
Hi! What is the weather today in Farenheight??
100.0
Would you like to convert this temperature to Celsius or Kelvin?
Celsius
Today's weather in Celsius is 37.7778 degrees Celsius!
Hi! What is the weather today in Farenheight??
100.0
Would you like to convert this temperature to Celsius or Kelvin?
Kelvin
Today's weather in Kelvin is 310.928 degrees Kelvin!
Press any key to continue . . .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With