我该如何停止重复这个答案?

时间:2016-12-01 21:47:01

标签: c++

我正在尝试编写一个家庭不和型游戏,我想知道如何阻止重复的答案?就像在这段代码中我可以继续写#"错误"并且循环将继续。

void f()
{
    int y=0;
    string q[17];
    string ans1[4];
    ans1[0]= "bears";
    ans1[1]="bugs";
    ans1[2]= "snakes";
    ans1[3]="skunks";
    string ans;
    int sum=0;
    q[0]="Name something you try to avoid when camping in the woods.";
    cout << q[0] << endl;
    for (int a=0; a<7; a++)
    {
        int b;
        getline(cin,ans);
        if (ans==ans1[0]||ans==ans1[1]||ans==ans1[2]||ans==ans1[3])
        {
           if (ans==ans1[0])
           {
               b=42;
            cout << "SURVEY SAYS " <<  b << "! Good Job! " << endl; sum=sum+b;
           }
           else if (ans==ans1[1])
           {
               b=33;
            cout << "SURVEY SAYS " <<  b << "! Nice one man! " << endl; sum=sum+b;
           }
           else if (ans==ans1[2])
            {
                b=20;
           cout <<"SURVEY SAYS " << b << "! Fantastic man!"<< endl; sum=sum+b;
            }

            else if (ans==ans1[3])
                {
                    b=5;
                    cout << "SURVEY SAYS " << b << "! You Got it!" << endl; sum=sum+b;
                }
        }
        else if (ans!=ans1[0]&&ans!=ans1[1]&&ans!=ans1[2]&&ans!=ans1[3])
        {
               cout << "YOU GOT THIS ONE WRONG! "<< endl; y++;
       if (y==3) { cout << "LOOOOSER" << endl; break;}
        }
    }
    cout << " your total score for

这一轮是&#34; &LT;&LT;总和&lt;&lt; ENDL; }

1 个答案:

答案 0 :(得分:1)

您需要保留所有已接受答案的清单(我建议std::vector)。然后,当给出新的答案时,您需要检查它是否在列表中。

或者,对于每个答案,都有一个wasUsed变量,您可以在给出答案时进行检查,并在接受答案后设置。