为什么我的代码不退出时,在命令提示符中输入3
Why is my code not exiting when Entering 3 in the Command prompt?
我是一个初学者,我正在上我的第一堂c++编程课。我们正在使用一个名为Codeblocks的IDE,我被难住了。我已经搜索了一个答案的论坛,但我只是不能弄清楚我自己。
我想弄清楚为什么我的代码不退出时,在命令提示符上的菜单中输入3。我也想弄清楚为什么当我试图把华氏温度转换成摄氏度时,我的公式不起作用,当我相信它是正确的公式时。
代码如下:
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main ()
{
float celsius;
float fahrenheit;
char x;
//Menu for user to choose which option they would like to
//preform. Also looping in case they type in the incorrect
// response and would like to choose again.
do
{
cout << "Please choose an option. Then please press Enter. n";
cout << "1. Convert Celsius to Fahrenheit.n";
cout << "2. Convert Fahrenheit to Celsius. n";
cout << "3. Exit Program n";
cin >> x;
if (x == '1')
system ("cls");
{
cout << "Please enter degrees in Celsius. n";
cin >> celsius;
system ("cls");
fahrenheit = 9.0 / 5 * celsius + 32;
//Calculate the formula for converting Celsius to Fahrenheit.
cout << fixed << showpoint << setprecision(2);
cout << fixed << "The degrees in Fahrenheit is n" << fahrenheit;
cout << static_cast<char>(248) << endl;
cout << "Thank you have a great day!";
(x = '3');
}
// User does not want to convert Celsius to Fahrenheit
// Since user does not want to convert, display a Thank you message.
if (x == '2')
{
cout << "Please enter degrees in Fahrenheit. n";
cin >> fahrenheit;
celsius = (fahrenheit - 32) * 5.0 / 9 ;
//Calculate the formula for converting Fahrenheit to Celsius.
cout << fixed << showpoint << setprecision(2);
cout << fixed << "The degrees in Celsius is n" << celsius;
cout << static_cast<char>(248) << endl;
cout << "Thank you have a great day!";
(x = '3');
} while (x != '3')
return 0;
}
}
您的代码格式不太好。当你在转换后结束程序时,我也不会得到do-while循环。
这是我的版本:
#include <iostream>
using namespace std;
int main()
{
float celsius;
float fahrenheit;
char x;
cout << "Please choose an option. Then please press Enter. n";
cout << "1. Convert Celsius to Fahrenheit.n";
cout << "2. Convert Fahrenheit to Celsius. n";
cout << "3. Exit Program n";
cin >> x;
if(x == '1')
{
cout << "Please enter degrees in Celsius. n";
cin >> celsius;
system("cls");
fahrenheit = 9.0 / 5 * celsius + 32;
printf("The degrees in Fahrenheit is %0.2f%cnThank you have a great day!", fahrenheit, (char)248);
}
else if(x == '2')
{
cout << "Please enter degrees in Fahrenheit. n";
cin >> fahrenheit;
system("cls");
celsius = (fahrenheit - 32) * 5.0 / 9;
printf("The degrees in Celsius is %0.2f%cnThank you have a great day!", celsius, (char)248);
}
else
{
return 0;
}
getchar();
getchar();
return 0;
}
我建议您查看一些c++代码风格指南。它的开发社区通常对他们想要做的事情非常严格。
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main () {
float celsius;
float fahrenheit;
char x = 0;
while (x != 3) {
cout << "Please choose an option. Then please press Enter. n";
cout << "1. Convert Celsius to Fahrenheit.n";
cout << "2. Convert Fahrenheit to Celsius. n";
cout << "3. Exit Program n";
cin >> x;
if (x == 1) { // You need to wrap if statement with brackets
cout << "Please enter degrees in Celsius. n";
cin >> celsius;
fahrenheit = 9.0f / 5.0f * celsius + 32;
//Calculate the formula for converting Celsius to Fahrenheit.
cout << fixed << showpoint << setprecision(2);
cout << fixed << "The degrees in Fahrenheit is n" <<fahrenheit;
cout << static_cast<char>(248) << endl;
cout << "Thank you have a great day!";
} // here is the first "if"s terminating bracket
// (x = '3'); I think you're trying to say ("if x == 3")? Which still isn't necessary
// there was an absurd amount of space here, should only be one or two lines
else if (x == 2) { // spaces around operator and again your "if" brackets were wrong
cout << "Please enter degrees in Fahrenheit. n";
cin >> fahrenheit;
celsius = fahrenheit - 32 * 5.0f / 9.0f; // both sides of a divisor should be double for clarity
//Calculate the formula for converting Fahrenheit to Celsius.
cout << fixed << showpoint << setprecision(2);
cout << fixed << "The degrees in Celsius is n" <<celsius;
cout << static_cast<char>(248) << endl;
cout << "Thank you have a great day!";
}
//(x = '3'); again, what is trying to be done here?
}
return 0;
}
在您的情况下,您可能不想使用do-while循环,因为您可以简单地零初始化x。
你也最好使用"if-else"语句,甚至"case-switch"控制语句(两者都可以谷歌;我已经展示了"else-if")
您也不处理无效输入,但这不是基本功能所必需的。
总是在控制语句和方法声明中缩进代码
if () {
int a = 0; // correct
int b = 1; // incorrect
}
可选的:您似乎还将左括号放在控制语句下面,如下所示:
if (x == 1)
{
// by some standards incorrect
}
// as opposed to
if (x == 1) {
// correct by most standards
}
你的(x = 3);
行只是说x = 3;
,如果你不输入1,它会立即终止你的循环(如果你要输入2,if (x == 1)
语句下面的x = 3;
赋值会在它到达if (x == 2)
之前终止你的程序。
对于初学者来说,你应该做的是,而不是拥有char x
,你应该让它成为int x
,因为你正在处理数字作为你的输入。这可能就是为什么你的代码不工作,因为你使用整数作为字符的值。你的代码应该像这样:
if(x == 2); // and so on for the rest of the places you use x;
至于您输入3作为x
的值的问题,在您的代码中,您给x
的值为3:
(x == '3')
,即使你不应该使用括号。你的代码应该像这样:
x = 3; // This is after you declare x as an int value
另一件你应该尝试做的事情是,不要使用
float celsius;
float fahrenheit;
try doing
double celsius;
double fahrenheit;
因为你处理的是double
数据类型。
我希望这是有帮助的,并有一个良好的编码时间。
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 如何使用命令提示符、记事本和 MinGW 使用主文件、头文件和实现文件编译C++程序?
- Windows 10命令提示符的输出不正确
- 命令提示符下的 NVCC CUDA 不使用 GPU
- 提供变量作为 MATLAB 系统命令的输入参数,以便C++可执行文件
- 使用makefile中的命令行输入
- C++构建增强 1.57.0 VS2013 x64 本机工具命令提示符
- 使用程序在C++中自动使用命令提示符
- C++使用命令行输入 txt 文件,但打开文件失败
- 从命令提示符使用 MSBuild 调试简单的C++可执行文件
- 在Windows命令提示符中使用G 编译C 程序
- C 错误:从命令行输入更改为变量输入
- 我将如何替换命令提示符上的文本?C
- 如何在IDE中而不是在命令提示符中显示Visual Studio控制台应用程序输出
- 窗口命令提示符输入说明
- 如何在Windows上通过命令提示符输入.exe文件
- 如果我不在命令提示符下输入 ARGC 值,如何初始化它?
- 如何使程序从gui而不是命令提示符中获取输入
- 为什么我的代码不退出时,在命令提示符中输入3
- 命令提示符关闭而不接受输入