2015-02-23 132 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