2017-08-02 162 views
-5

我在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

回答

0

在每個if語句周圍放置括號以結束if語句,並取出break語句,因爲它們結束了while循環,而不是if語句。

+0

你爲什麼回答你自己的問題? –

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

在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塊。

0

只要刪除所有break,你很好去。 if陳述很好地縮進,沒有問題。