在命令提示符下输入3时,为什么我的代码没有退出?

时间:2015-10-03 01:28:47

标签: c++ codeblocks

我是初学者,我正在学习第一个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;

    }
}

3 个答案:

答案 0 :(得分: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%c\nThank 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%c\nThank you have a great day!", celsius, (char)248);
    }

    else
    {
        return 0;
    }

    getchar();
    getchar();
    return 0;
}

答案 1 :(得分: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循环,因为您只需zero-initialize x

你也最好使用“if-else”语句甚至“case-switch”控制语句(两者都可以用Google搜索;我已经显示“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,x = 3;下面的if (x == 1)分配if (x == 2)声明会在您的计划到达{{1}}之前终止该计划。

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

答案 2 :(得分:-1)

对于初学者来说,你应该做的不是char x,而应该让它int x,因为你在输入时处理数字。这可能是您的代码无效的原因,因为您使用整数作为chars的值。您的代码应如下所示:

if(x == 2); // and so on for the rest of the places you use x;

至于输入3作为x的值的问题,在您的代码中,您给了x三个值,如下所示:

(x == '3') 

即使你不应该使用括号。您的代码应如下所示:

x = 3; // This is after you declare x as an int value

您应该尝试的另一件事是使用

float celsius;
float fahrenheit;

尝试做

double celsius;
double fahrenheit;

因为您正在处理double数据类型。

我希望这很有帮助并且编码很好。