2015-02-23 35 views
0

說你的代碼是:C++如何重新啓動if語句?

cout << "You wake up in a room. There is a small lit candle and a door. What do you do?" << endl; 
     string startout; 
     getline(cin, startout); 
      if (startout == "Door" || startout == "DOOR" || startout == "door" || startout == "Open Door" || startout == "open door" || startout == "OPEN DOOR" || startout == "Open door" || startout == "Open the door" || startout == "open the door") { 
       cout << " " << endl; 
       cout << "You move to the door but its too dark to see anything besides the outline of it." << endl; 
      } else if (startout == "Candle" || startout == "CANDLE" || startout == "candle" || startout == "Pick Up Candle" || startout == "PICK UP CANDLE" || startout == "pick up candle" || startout == "Pick up candle") { 
       cout << "You pick up the candle then move to the door. With the light from the candle you can see the door well. What do you do?" << endl; 
      }; 

如果使用的代碼類型的東西,除了「門」或「蠟燭」(或任何變體),還可以將代碼是什麼重啓if語句,因此重新誰是 - 問第一個問題?

例如:用戶輸入:「跳舞」

輸出:「我不明白,‘跳舞’。 你是做什麼?」

或類似的東西。

+11

所以...你想要一個「循環」 – 2015-02-23 18:18:20

+1

我是在Reddit上幫助你的人 - 一旦你開始將它應用到你的樹的每一個分支上,它就會變得非常難看。你真的需要開始研究使用有限狀態機而不是真正深層的if語句樹。 – 2015-02-23 18:32:23

+0

你也應該把你的輸入變成小寫,然後你只需要比較'startout ==「門」||「。 startout ==「開門」,而不是每個大寫字母的組合。 – 2015-02-23 18:33:13

回答

0

您可以在外部添加一個while(true)循環,並添加if條件,該條件在正確輸入時會中斷。

另外,函數getline命令之後,如果別的conditon之後添加其他條件:

else 
    cout << "I don't understand " << startout << ". What do you do?" << endl; 

在其他ifelse if子句,添加一個break;命令。

0

你想運行,直到他們鍵入正確的東西?這很簡單。你應該使用一個循環。

循環通常是一個重複自己,直到條件滿足爲止的語句塊。例如,對你來說一個好的循環是:

Repeat the program until the string equals Door or Candle

條件,我看你已經很親近了。然後,讓我向您介紹while循環的塊的語法,我認爲它最適合您的情況(do while是適合您的另一個循環,但解釋稍微複雜一點)。 編輯:我改變雨水的評論代碼:

string startout=""; // Initiallizing it before the while loop, so it could be used inside it. 
cout << "You wake up in a room. There is a small lit candle and a door. What do you do?" << endl; 
while(startout !="door" && startout !="candle") // Do the following actions in this block until startout equals "Door" or "Candle". 
{ // start of a while block 
    getline(cin, startout); 
    if (startout.tolower().find("door")!=npos) // If the string contains any variation of door: 
    { 
     cout << " " << endl; 
     cout << "You move to the door but its too dark to see anything besides the outline of it." << endl; 
    } 
    else if (startout.tolower().find("candle")!=npos) // same, with candle. Much more elegant, like thomas said. 
    { 
      cout << "You pick up the candle then move to the door. With the light from the candle you can see the door well. What do you do?" << endl; 
    } 
    else 
    { 
     cout << "I don't understand " << startout << ". What do you do?" << endl; // If you didn't get what you want, ask for another string and restart. 
    } 
} // end of the while block. 
+1

您可以通過將文本轉換爲全部小寫或全部大寫來減少if語句。這會減少'if'語句中的表達式的數量爲1. – 2015-02-23 18:30:08

+0

@ThomasMatthews就這樣做了,加了一點點(:好主意。 – 2015-02-23 18:34:47

+0

)鑑於長期的,他可能會想要更多不同的動作,我會使用地圖,而不是一連串的「if」,而且我肯定會將實際行爲放在循環之外 – 2015-02-23 18:41:23

-2
for (;;) 
    cout << "You wake up in a room. There is a small lit candle and a door. What do you do?" << endl; 
    string startout; 
    getline(cin, startout); 
    if (startout == "Door" || startout == "DOOR" || 
     startout == "door" || startout == "Open Door" || 
     startout == "open door" || startout == "OPEN DOOR" || 
     startout == "Open door" || startout == "Open the door" || 
     startout == "open the door") { 

     cout << " " << endl; 
     cout << "You move to the door but its too dark to see anything besides the outline of it." << endl; 
     break; 
    } else if (startout == "Candle" || startout == "CANDLE" || 
       startout == "candle" || startout == "Pick Up Candle" || 
       startout == "PICK UP CANDLE" || 
       startout == "pick up candle" || 
       startout == "Pick up candle") { 
     cout << "You pick up the candle then move to the door. With the light from the candle you can see the door well. What do you do?" << endl; 
     break; 
    }; 
} 

不是最優雅的方式來做到這一點,但你的想法。

+0

使用無限循環稍微比不高雅 - 他幾乎給了你這個詞 – 2015-02-23 18:28:47

+0

@A。阿布拉莫夫哦,來吧,這是一個基於命令行的RPG遊戲,從整個遊戲的角度來看,整個遊戲就是一個無限循環,在遊戲結束的時候會打破這個循環。傷害 – user3109672 2015-02-23 18:31:55

+0

我的觀點非常強烈 - 這是一款RPG遊戲,因此他可能希望在循環完成後做些事情,但是你的解決方案並不是錯的 - 但它也是不對的 – 2015-02-23 18:35:53

1

首先,你需要一個更好的解析器。至少,將 合法值放在表格或某種地圖中,並指向 操作。然後換行輸入的功能:

typedef void (*  ActionPointer)(); // Or whatever you need. 
typedef std::map< std::string, ActionPointer, CaseInsensitiveCmp > 
        ActionMap; 

ActionPointer 
getAction(ActionMap const& legalValues) 
{ 
    std::string line; 
    if (! std::getline(std::cin)) { 
     // Error on std::cin... Probably fatal. 
    } 
    ActionMap::const_iterator action = legalValues.find(); 
    return action == legalValues.end() 
     ? nullptr 
     : action->second; 
} 

然後,你可以寫類似:

std::cout << "You wake up in a room. There is a small lit candle and a door." 
      " What do you do?" << std::endl; 
ActionPointer nextAction = getAction(); 
while (nextAction == nullptr) { 
    std::cout << "I don't understand. What do you do?" << std::endl; 
    nextAction = getAction(); 
} 
(*nextAction)(); 

如果你想在錯誤信息的更多信息,你可以安排 回報struct與指針和那個信息(還在測試 的指針)。

+1

看起來像一個翻譯....漂亮整潔;然而,我不確定所有的指針和地圖是否適合尚未學習循環的人... – 2015-02-23 18:37:01

+0

@ A.Abramov地圖對他來說可能是新的,但他絕對應該用它們來代替他笨重的「如果」。如果你不習慣,指向函數的指針可能有點棘手。這是我使用的解決方案,但是您可以用'enum'和'switch'輕鬆替換它。然而,最重要的一點是,這至少需要兩種不同的功能。 – 2015-02-23 18:39:38

+0

姆姆,你說服了我。雖然我必須提到'if's很容易改變 - 當它僅僅是這兩個命令時,你可以使它們看起來更好。 – 2015-02-23 18:41:57