-2

当按“1”开始游戏时,首先出现错误消息而不是玩游戏,然后我必须在游戏开始前输入“1”3次,并且退出游戏选项仅在您先选择“2”时才有效如果它没有首先被选中,它只是作为错误消息出现,我看不出它为什么会这样,任何人都可以帮助我吗?

#include "Questions.h"
#include <iostream>
using namespace std;

const int MAXITEMS = 10;

int main ()
{
    string question[MAXITEMS] = {"How man cards in a suit",
      "How_many_suits_are_there_in_a_standard_pack_of_card",
      "How_many_kings_are_in_a_standard_pack_of_cards"};
    string answers[MAXITEMS] = {"4", "5", "6"};

    int userInput = 0;
    int tries = 0;


    bool isGameOver = false;

    cout << "select 1 to start game" << endl;  //gives option to start and quit game
    cout << "select 2 to quit game" << endl;
    cin >> userInput;

    if (userInput == 2)
    {
        isGameOver = true;
        return 0;   
    };
    // when game starts gives option to select question and shows all questions
    do
    {
        if (userInput != 1||2)
        {
            cout << " Your input is not valid! please try again:" << endl; 
                          // try switch cases for the different outcomes

            cout << "select 1 to start game" << endl;  
            cout << "select 2 to quit game" << endl;
            cin >> userInput;

            while (!(cin >> userInput))
            {
                cin.clear(); // clear the error flags
                cin.ignore(INT_MAX, '\n'); // discard the row
                cout << "Your input is not valid! please try again: ";

                cout << "select 1 to start game" << endl;  
                cout << "select 2 to quit game" << endl;
            }
            cout << userInput << endl;



        }
        // reprisent all characters as number to stop while roblem

        if(userInput == 1)
        {
            do
            {
                cout << "select question" << endl;

                for(int i = 0; i != MAXITEMS; i++)
                {
                    cout << i << " " << question[i] << endl;
                }

                int selectQestion;
                cin >> selectQestion;

                if(selectQestion == 0||1||2  && tries != 2)
                {
                    cout << "Enter your answer" << endl;
                    string userAnswer;

                    cin >> userAnswer;

                    while (!(cin >> userAnswer))
                    {
                        cin.clear(); // clear the error flags
                        cin.ignore(INT_MAX, '\n'); 
                                                        // discard the row
                        cout << "Your input is not valid!
                                                                  please try again: ";
                    }

                    if (userAnswer == answers[0])
                    {
                        cout << "Correct answer" << endl;
                    }
                    else{

                        cout << "incorrect try again" << endl;
                        tries++;

                        cin >> userAnswer;
                        if (userAnswer == answers[0])
                        {
                        cout << "Correct answer" << endl;
                        }
                        else 
                        cout << "Incorrect" << endl;

                    }
                }
                if (selectQestion == 0 ||1 ||2  && tries == 2)
                {
                cout << "you can no longer answer this question" << endl;
                cout << "try another question" << endl;
                }

            }
            while(userInput == 1);

        }
        }
    while(isGameOver == false);

}
// add stuct or class to handle questions, 
4

2 回答 2

3

问题出在这里:

if (UserInput!=1||2)

在这一行中,有两个条件:

UserInput!=1 , 2

这里,无论用户输入是 1/2,第二个条件 2总是被评估为true,它运行 if 块

所以改成

if (UserInput!=1 && UserInput!=2) 
于 2013-04-26T22:24:58.580 回答
3

if (userInput != 1||2)不按你的想法做。插入适当的括号,它是

if ((userInput != 1) || 2)

并且2是非零的,因此条件始终为真。

你要

if (userInput != 1 && userInput != 2)
于 2013-04-26T22:22:25.920 回答