【问题标题】:Im creating a text based adventure game and need an alternative to using GOTO我正在创建一个基于文本的冒险游戏,需要替代使用 GOTO
【发布时间】:2014-09-16 15:57:03
【问题描述】:

如何正确使用 do、while 或 for 循环来防止用户输入答案“1”或“2”以外的任何其他内容?

如果他们不这样做,程序应该告诉他们他们不能这样做,然后让他们回到上一个问题。

#include <iostream>
#include "Options.h"
#include <string>

using namespace std;

int main(int argc, char *argv[]) {
    cout << "\tWelcome to my text adventure game\n" << endl;
    cout << "Please enter your characters Name: ";
    string name;
    cin >> name;
    cout  << "\nWelcome " << name << endl;
    cout << "\nPlease choose the side you would like to play in - ";
    Options OptionsObject;
    OptionsObject.optionsSide();

    string answer;
    while(answer != "quit") {
        cin >> answer;
        cout << answer << endl;
    }

    if ( answer == "1" ) {
        cout << "You chose the good side. Let the game begin\n "  << endl;
        cout << "You are located in a city named after the warrior who saved it from the evil\nmany years ago. The city of Redshore. " << endl;
        cout << "You are no ordinary man in the City of Redshore. You are the king who rules it\nYou are seen as King of Justice, a good king.\nOne who only want what is best for his people" << endl;
        cout << "but also a troubled man. You experienced something traumatizing when you\nwere a just a little boy, but no one knows about it,\nno one but yourself that is  " << endl;
    } else if( answer == "2" ) {
        cout << "hey there" << endl;
    }
}

【问题讨论】:

标签: c++ loops goto


【解决方案1】:

看到这个问题会出现在用户在游戏过程中会做的所有选择中,我会做一个外部函数,让用户选择。也许是一个以这种方式工作的静态类。

int ans = Answer.getUserAnswer(2,"[1/2]>");

这应该以这种方式工作:

public static int getUserAnswer(int max =1,string message="[1/1]>")
{
    int ans = 0;
    while(ans==0){
        cout<<message<<" ";
        cin >> answer;
        if(answer>0 and answer<=max) return ans;
        cout<<"\tnot a valid choose\n";
        ans=0;
    }
}

并且您将在您的 ans 中获得您期望的值之一,如果答案不在您期望的值之间,它将打印“无效选择”,即使有 10 个答案,您也可以这样做,调用它默认情况下,希望您只给他第一名。

我在自己制作的主机游戏中使用了它;) 希望有用

编辑

int getUserAnswer(int max =1,string message="[1/1]>")
{
    int ans = 0;
    while(ans==0){
        cout<<message<<" ";
        cin >> answer;
        if(answer>0 and answer<=max) return ans;
        cout<<"\tnot a valid choose\n";
        ans=0;
    }
}

并在您的代码中请求此代码:

cout  << "\nWelcome " << name << endl;
    cout << "\nPlease choose the side you would like to play in - ";
    Options OptionsObject;
    OptionsObject.optionsSide();

    int answer =getUserAnswer(2,"[1/2]>");

    if (answer == 1){

cout << "You chose the good side. Let the game begin\n "  << endl; cout << "You are located in a city named after the warrior who saved it from the evil\nmany years ago. The city of Redshore. " << endl; cout << "You are no ordinary man in the City of Redshore. You are the king who rules it\nYou are seen as King of Justice, a good king.\nOne who only want what is best for his people" << endl; cout << "but also a troubled man. You experienced something traumatizing when you\nwere a just a little boy, but no one knows about it,\nno one but yourself that is  " << endl;
    }

    else if(answer == 2){

        cout << "hey there" << endl;

    }

【讨论】:

  • 我完全不知道这段代码发生了什么,我还没有深入了解公共静态的含义,而且我不太确定如何从其他类中“获取”一些东西
  • 那么在你的 main 中使用它作为一个函数,不是正确的方法......但它可以工作。只是拿走了 public static 并按原样使用 getUserAnswer 函数。在问题之后的代码中,您应该像使用它一样使用它 int ans = getUserAnswer(2,"[1/2]>");其中第一个参数是用户可以给你的最大答案,第二个是每次迭代显示的提示。试试看
  • 为什么循环的最后一行是赤裸裸的比较? ans==0;
  • 因为我犯了一个错误 :D 必须是 ans = 0;
【解决方案2】:

您应该将读取输入的行放在一个 do 循环中,该循环在用户给出的答案无效时循环。

所以,你的 do 循环应该是:

string answer;

cin >> answer;

// loop while answer is neither "1" nor "2"
while( answer != "1" && answer != "2" ) {
    cout << "Please enter a 1 or a 2. You entered " << answer << endl;
    cin >> answer;
}

【讨论】:

  • 或者使用 do...while,这在这里看起来更合乎逻辑(总是需要读取一次)。
  • 我尝试实现它,我很确定我没有正确地做到这一点。现在,当我输入除 1 或 2 之外的任何其他内容时,程序不会像以前那样关闭,但什么也没有发生,我可以永远输入数字。
  • 在用户输入“1”或“2”后,您有点暗示他们输入了其他内容。
猜你喜欢
  • 2016-08-25
  • 2013-07-08
  • 2018-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
相关资源
最近更新 更多