在c ++程序中输入错误

时间:2013-04-26 22:19:57

标签: c++

当按“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, 

3 个答案:

答案 0 :(得分:3)

if (userInput != 1||2)没有按你的想法行事。插入适当的paretheses后,它就是

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

2非零,因此条件始终为真。

你想要

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

答案 1 :(得分:3)

问题在于:

if (UserInput!=1||2)

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

UserInput!=1 , 2

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

所以将其改为

if (UserInput!=1 && UserInput!=2) 

答案 2 :(得分:0)

不要检查这样的情况

if (userInput != 1||2) // It means if (userInput != 1) OR 2, which is always true

试试这个:

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

其他方面的类似问题......