程序在输入开尔文或摄氏后终止?(我的代码有什么问题?
Programs terminates after input Kelvin or Celsius? (What's wrong with my code?)
****它现在工作的方式我想要的,谢谢大家!:):)
谢谢!摄氏度现在可以用了,但为什么开尔文仍然是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 . . .
声明变量Celsius
和Kelvin
;但是,它们的默认值设置为""
(空字符串)。你忘记初始化它们了
如果你想让你的程序工作,你需要像这样初始化这些变量为它们的期望值(可能是"Celsius"
和"Kelvin"
):
string Celsius = "Celsius", Kelvin = "Kelvin";
然而,更好的解决方案是不为这两个选项创建任何变量,并在条件中直接与字符串字面值进行比较。这样的:
if(input == "Celsius")
{
...
}
else if(input == "Kelvin")
{
...
}
我还注意到另一件事。您正在计算celsius
和kelvin
的值,然后将它们打印出来。
- 如何改进我的代码,使其不会因超时而终止?(黑客排名挑战)
- 为什么我的 while 函数没有终止?
- 为什么我的显示功能会终止程序?
- PostQuitMessage(0) 不会终止我的"程序"
- 这是我的 .h 文件的一些错误,当我在其中包含我的类模板时,它显示 [错误] 未终止 #ifndef
- 当我输入非整数值时,我的二次方程代码终止,如何让它正确循环?
- 为什么我的 C++ 应用以退出值 -1 终止,但某些代码逻辑未执行?
- 我正在尝试计算 char 数组中的内容,直到 null 终止,但每次编译时,我都会得到一个比我的数组大的数字
- 为什么我的析构函数中的异常没有触发 std::终止?
- 尝试将未知数量的字符串推送到向量中,但我的 cin 循环没有终止
- 我的链接列表程序在代码块编译器(X0000000005)中以状态-1073741819终止
- 对我是否需要一个哨兵值来终止我的 do while 循环以允许多个条目感到困惑
- 为什么我的 c++ 程序在 Eclipse 中没有输出而终止,但在 Visual Studio 中工作
- 为什么我的程序在执行后立即终止
- 为什么我的简单C++项目在我可以看到之前就终止于日食?我如何让它停止
- 我的Qt C++程序以一种不同寻常的方式终止
- 当我采用测试用例数 = 1 时,我的程序终止.其他明智的是,它给出了正确的答案,但跳过了最后一个测试用例
- 当我的程序终止时,我应该总是在COM指针上调用Release吗
- 如何通过按特殊字符"!"在 c++ 上终止我的程序?
- 我如何终止我的程序使用ESC按钮