程序在输入Kelvin或Celsius后终止? (我的代码有什么问题?)

时间:2015-09-06 00:18:43

标签: c++

****它现在按照我想要的方式工作,谢谢大家! :) :)

***谢谢! Celsius现在有效,但为什么开尔文仍然变为0?

**谢谢!我似乎已经解决了在进入" kelvin"之后程序结束的问题。或者"摄氏度"。我还在我的代码中更改了华氏温度的拼写。现在,问题是我得到的答案总是0,而不是转换......

原始代码

//  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;

}

原始代码

的问题

我试图这么做,所以它要求输入你想要的华氏度数,然后询问你是否要将其转换为摄氏或开尔文,然后继续按照等式给出转换。然而,在输入后我似乎要结束我想要Kelvin或Celsius - 即使我只是使用等式,答案总是出现为0 ......

输出是:

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)

2 个答案:

答案 0 :(得分:5)

我已经纠正了(在这里松散地使用这个词)您共享的代码。与评论中提到的其他人一样,您的变量名称拼写错误,并且未使用值初始化。

另一个错误是你使用整数除法(farenheight + 459.67)*(5 / 9);将5除以9。这会导致类型强制,因为两个操作数都是int类型,结果被强制转换为int,删除小数位后的任何数字。在这种情况下,5/9结果为0,因此您的整个结果将乘以0.

导致结果不正确的另一个问题是,在声明变量应包含的内容之前,您要将变量输出到流中。如果这样做,无论存储在内存中的是什么,空字符串都将输出到屏幕。

以下是更正后的代码:

//  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;
    }
}

这里有两个输出示例:

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 . . .

答案 1 :(得分:4)

您声明了名为CelsiusKelvin的变量;但是,它们的默认值设置为""(空字符串)。你忘了初始化它们。

如果您想让您的程序正常工作,您需要将这些变量初始化为其预期值(可能是"Celsius""Kelvin"),如下所示:

string Celsius = "Celsius", Kelvin = "Kelvin";

然而,这里更好的解决方案是不要为这两个选项制作任何变量,并在条件中直接与字符串文字进行比较。像这样:

if(input == "Celsius")
{
...
}
else if(input == "Kelvin")
{
...
}

我注意到的另一件事。您在打印后计算celsiuskelvin的值。你应该以前做。