为什么我的代码不退出时,在命令提示符中输入3

Why is my code not exiting when Entering 3 in the Command prompt?

本文关键字:命令提示符 输入 我的 代码 退出 为什么      更新时间:2023-10-16

我是一个初学者,我正在上我的第一堂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)之前终止你的程序。

总之,代码样式被大多数人认为是最重要的原则之一。我强烈建议你阅读并遵守一套严格的规则。如Google的c++风格指南

对于初学者来说,你应该做的是,而不是拥有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数据类型。

我希望这是有帮助的,并有一个良好的编码时间。