无限虽然循环不工作C++

问题描述:

我在C++编码,并认为我会为我的妹妹创造一个游戏。但是,当我运行我的代码时,它只运行一次,即使我有一个while循环应该使其运行无限次。我正在使用代码块和GCC GNU编译器。当我运行的代码,它的if语句之后结束后不运行COUT要么... 下面是代码(这是我的妹妹,所以它可能是哑):无限虽然循环不工作C++

#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; 
} 
+2

你认为'break'声明有什么用处? –

+1

你错过了'if'语句正文的大括号。 –

+0

它结束如果对 – sp1d3rcr3w1an

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

+0

你为什么回答你自己的问题? –

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

如果if语句中的最后一行被执行,if语句将自动终止。

在你的情况下,使用'break'语句终止while循环,而不是if语句,这会导致你的程序只运行一次。

其次,尝试删除break语句并向每个if语句添加一组“{}”。

您还可以在第一个if语句之后的if语句中添加'else'语句,这会使程序更高效。例如,如果第一个if语句是真的,那么就没有必要if语句来检查其他,因为它们将不需要执行

例如...

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

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

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

此外,而不是使用在while循环中进行比较,尝试使用'true'语句。

#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; 
} 

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

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

这将调用功能foobar当且仅当是conditiontrue当转换为一个bool返回true

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

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

这里如果conditiontrue,但不管conditionbar执行foo时才执行。所以编写一组互斥块的方法之一是做这样的事情。

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块。

只要删除所有break,你很好去。 if陈述很好地缩进,没有问题。