无限循环不工作C ++

时间:2017-08-02 01:46:25

标签: c++ while-loop

我用C ++编写代码,并认为我会为我的妹妹创建一个游戏。但是,当我运行我的代码时,它只运行一次,即使我有一个while循环,它应该使它运行无限次。我正在使用代码块和GCC GNU编译器。当我运行代码时,它在if语句之后结束,并且在...之后不运行cout ... 这是代码(这是给我姐姐的,所以可能是愚蠢的):

#include <iostream>

using namespace std;

int main(){
    cout << "Welcome to Uni Game" << endl;
    cout << "the fun unicorn game!" << endl;
    string riding;
    int happiness = 0;
    while (0 == 0)
    {
        cout << "Would you like to a: ride a unicorn b: feed your unicorn c: lead your unicorn? ";
        cin >> riding;
        if (riding == "a")
            cout << "You jump onto your unicorn!" << endl;
            cout << "You ride your unicorn through the park, seeing loads of flowers!" << endl;
            cout << "After your horse eats loads of flowers, you head back to the stable." << endl;
            happiness = happiness + 3;
            break;
        if (riding == "b")
            cout <<  "You feed your unicorn!" << endl;
            cout << "You give it its favorite wheat, which it absolutely loves!" << endl;
            happiness = happiness + 2;
            break;
        if (riding == "c")
            cout << "You grab a lead for your unicorn!" << endl;
            cout << "You head to the paddock and lead your unicorn for a walk." << endl;
            cout << "Your unicorn loves this!" << endl;
            happiness = happiness + 1;
            break;
        cout << "Your happiness level is: " << happiness;
    }
    return 0;
}

4 个答案:

答案 0 :(得分:4)

在C ++中,可以通过以下方式编写多行条件块。

if (condition) {
    foo();
    bar();
}

当且仅当foobar时才会调用函数conditiontrue,或在转换为true时返回bool

如果省略括号,则只有一个命令由条件控制。

if (condition)
    foo();
bar();

此处foo仅在conditiontrue时执行,但无论bar的值如何,都会执行condition。因此,编写一组互斥块的一种方法是做这样的事情。

if (riding == "a") {
    std::cout << "Fun text." << std::endl;
    std::cout << "More text." << std::endl;
}
if (riding == "b") {
// etc.

请注意缺少breakbreak做了一些无关的事情。它结束了它所调用的循环。这就是你的循环停止的原因。

您可以通过添加一些else关键字来提高上述代码的效率,以防止检查您知道的相互排斥的条件。

if (riding == "a") {
    // Somethin'.
} else if (riding == "b") {
    // Somethin' else.
} else if (riding == "c") {
    // Somethin' much more else.
} else {
    std::cerr << "Unrecognized option '" << riding << "'." << std::endl;
    return 1;
}

如果您的每个选项的长度只有一个字母,您还可以ridingchar并利用switch语法。

switch (riding) {
    case 'a':
        // Something.
        break;
    case 'b':
        // Something.
        break;
    case 'c':
        // Something.
        break;
    default:
        std::cout << riding << "?  Hey, buddy, no one tells me to " << riding << "." << std::endl;
        return 1;
}

现在您恢复了break个关键字。 break也用于结束switch阻止。

答案 1 :(得分:0)

在每个if语句周围加上括号以结束if语句,并取出break语句,因为它们结束了while循环,而不是if语句。

答案 2 :(得分:0)

首先,您不需要break语句来终止if语句。

if语句将在执行其中的最后一行代码后自动终止。

在您的情况下,使用&#39; break&#39;语句终止while循环,而不是if语句,这导致程序只运行一次。

其次,请尝试删除break语句并添加一组&#39; {}&#39;每个if语句。

您还可以添加其他&#39;在第一个if语句之后的if语句的语句,这使程序更有效。例如,如果第一个if语句为true,则不需要检查其他if语句,因为它们不需要执行

例如......

    if(riding == a)
    {
        //Execute some code...
    }

    else if(riding == b)
    {
        //Execute some code...
    }

    else if(riding == c)
    {
        //Execute some code...
    }

此外,请尝试使用&#39; true&#39;而不是在while循环中使用比较。言。

    #include <instream>

using namespace std;

int main(){
    cout << "Welcome to Uni Game" << endl;
    cout << "the fun unicorn game!" << endl;
    string riding;
    int happiness = 0;

    while (true)
    {
        cout << "Would you like to a: ride a unicorn b: feed your unicorn c: lead your unicorn? ";
        cin >> riding;

        if (riding == "a")
        {
            cout << "You jump onto your unicorn!" << endl;
            cout << "You ride your unicorn through the park, seeing loads of flowers!" << endl;
            cout << "After your horse eats loads of flowers, you head back to the stable." << endl;
            happiness = happiness + 3;
        }

       else if (riding == "b")
       {
            cout <<  "You feed your unicorn!" << endl;
            cout << "You give it its favorite wheat, which it absolutely loves!" << endl;
            happiness = happiness + 2;
       }

       else if (riding == "c")
       {
            cout << "You grab a lead for your unicorn!" << endl;
            cout << "You head to the paddock and lead your unicorn for a walk." << endl;
            cout << "Your unicorn loves this!" << endl;
            happiness = happiness + 1;

        cout << "Your happiness level is: " << happiness;
       }
    }
    return 0;
}

答案 3 :(得分:0)

只需删除所有break,您就可以了。 if语句很好地缩进,没有问题。