程序在输入开尔文或摄氏后终止?(我的代码有什么问题?

Programs terminates after input Kelvin or Celsius? (What's wrong with my code?)

本文关键字:终止 我的 代码 问题 什么 输入 程序      更新时间:2023-10-16

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

谢谢!摄氏度现在可以用了,但为什么开尔文仍然是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, 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;
}

原始代码的问题

我试着让它问输入你想要多少华氏度,然后问你是想把它转换成摄氏度还是开尔文,然后继续按照方程进行转换。然而,它似乎结束后,我输入我想要开尔文或摄氏度-即使我只是使用方程,答案总是出来为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)

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

另一个错误是您使用整数除法(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 . . .

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

如果你想让你的程序工作,你需要像这样初始化这些变量为它们的期望值(可能是"Celsius""Kelvin"):

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

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

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

我还注意到另一件事。您正在计算celsiuskelvin的值,然后将它们打印出来。

相关文章: