超时错误 C++
Timeout error C++
#include <iostream>
// #include <conio>
using namespace std;
int main(){
int choice;
double temp, result;
std::cout <<"Enter temprature";
std::cin>>temp;
std::cout<<"Enter number between 1 or 2";
std::cin>>choice;
switch (choice){
case 1 :
result = (5*temp /9) - 32;
std::cout<<"answer is" <<result<<"C \n";
break;
case 2 :
result = (9/5*temp) + 32;
std::cout <<"answer" <<result<<"F";
break;
default : std::cout<<"Wrong number";
}
return 0;
}
我正在使用此代码进行基本转换(切换练习),出现超时错误。我是 cpp 的新手,如果您觉得这很简单,请不要介意。
此外,如何在此程序中使用 conio.h 函数,如 getch()?
我遇到了这个错误:
Enter temprature
Timeout - Some common reasons for Timeout
Your Program may have a endless loop
您的代码在 MSVS 2015 中编译,似乎可以正常工作。
这个:
result = (5*temp /9) - 32;
不是正确的公式。应该是
result = 5 * (temp - 32) / 9;
这个:
result = (9/5*temp) + 32
;
整数溢出。应该是:
result = (9.0 / 5 * temp) + 32;
或
result = (9 * temp / 5) + 32;
How can i use conio.h functions like getch() in this program?
conio.h
不是标准的 header 并且 getch()
不是标准的函数,因此尽管您可以在某些平台上使用它们,但它们在其他平台上不可用.
啊,终于找到错误了。看来您正在使用 JDoodle,并且没有向标准输入提供任何输入!这很容易解决。只需单击 Stdin Inputs...
框,然后在第一行输入第一个提示,在第二行输入第二个提示。这是一个示例屏幕截图:
唯一的问题是每个输出都在同一行,因此您可能需要在代码中添加一些换行符。
作为第二种解决方案,您可以打开交互模式(标准输入输入正上方的按钮),这实际上会在 运行 程序时要求您输入。此解决方案可能更适合您的需求,因为它更有意义。
就 conio.h
而言,您将无法使用 header 中的函数(这很好,因为它们是非标准的),除非您在本地使用 Windows 电脑.
#include <iostream>
// #include <conio>
using namespace std;
int main(){
int choice;
double temp, result;
std::cout <<"Enter temprature";
std::cin>>temp;
std::cout<<"Enter number between 1 or 2";
std::cin>>choice;
switch (choice){
case 1 :
result = (5*temp /9) - 32;
std::cout<<"answer is" <<result<<"C \n";
break;
case 2 :
result = (9/5*temp) + 32;
std::cout <<"answer" <<result<<"F";
break;
default : std::cout<<"Wrong number";
}
return 0;
}
我正在使用此代码进行基本转换(切换练习),出现超时错误。我是 cpp 的新手,如果您觉得这很简单,请不要介意。
此外,如何在此程序中使用 conio.h 函数,如 getch()? 我遇到了这个错误:
Enter temprature
Timeout - Some common reasons for Timeout
Your Program may have a endless loop
您的代码在 MSVS 2015 中编译,似乎可以正常工作。
这个:
result = (5*temp /9) - 32;
不是正确的公式。应该是
result = 5 * (temp - 32) / 9;
这个:
result = (9/5*temp) + 32
;
整数溢出。应该是:
result = (9.0 / 5 * temp) + 32;
或
result = (9 * temp / 5) + 32;
How can i use conio.h functions like getch() in this program?
conio.h
不是标准的 header 并且 getch()
不是标准的函数,因此尽管您可以在某些平台上使用它们,但它们在其他平台上不可用.
啊,终于找到错误了。看来您正在使用 JDoodle,并且没有向标准输入提供任何输入!这很容易解决。只需单击 Stdin Inputs...
框,然后在第一行输入第一个提示,在第二行输入第二个提示。这是一个示例屏幕截图:
唯一的问题是每个输出都在同一行,因此您可能需要在代码中添加一些换行符。
作为第二种解决方案,您可以打开交互模式(标准输入输入正上方的按钮),这实际上会在 运行 程序时要求您输入。此解决方案可能更适合您的需求,因为它更有意义。
就 conio.h
而言,您将无法使用 header 中的函数(这很好,因为它们是非标准的),除非您在本地使用 Windows 电脑.