簡體   English   中英

初學者 C++ 使用 cin.fail() 防止無效輸入

[英]Beginner C++ Protecting against invalid input with cin.fail()

我剛剛完成了這個大理石游戲程序的工作,它運行良好,但我需要再做一件事來保護它免受無效輸入的影響。 該程序正確響應所有基於整數的無效輸入,但如果您輸入非整數,程序將崩潰。 我一直在使用 cin.fail() 函數,它可以工作,但隨后陷入循環。 如果用戶輸入一個非整數,我希望程序告訴用戶輸入無效並提示用戶再次輸入他們的選擇。

我加了 return 1; 在無效輸入語句之后結束程序而不是崩潰,但這不是我想要做的。 我希望它打印“無效輸入”,然后重新提示用戶再次輸入他們的選擇。

#include <iostream>
using namespace std;

int main()
{
    cout << "*** The Game of Nim ***\n\n" << endl;

    string player1, player2, currentPlayer;
    int marbles, selection;
    bool isDone = false;

    cout << "Enter a name for Player 1: ";
    cin >> player1;
    cout << "Enter a name for Player 2: ";
    cin >> player2;
    cout << "Enter how many marbles you would like to start with in the pile: ";
    cin >> marbles;

    cout << "\nThere are " << marbles << " marbles in the pile\n" << endl;

    currentPlayer = player1;

    do
    {
        bool turnDone = false;

        while (turnDone == false && marbles != 0)
        {
            if (currentPlayer == player1)
            {
                cout << currentPlayer << "\n";

                cout << "How many marbles would you like to draw? : ";
                cin >> selection;

                if (cin.fail())
                {
                    cout << "\nInvalid Input\n";
                    return 1;

                }


                if (selection < marbles / 2 + 1 && marbles > 1 && selection != 0)
                {
                    marbles = marbles - selection;

                    if (marbles == 1)
                    {
                        cout << "\nUh Oh! There is only 1 marble remaining in the pile\n" << endl;

                        currentPlayer = player2;
                        turnDone = true;
                        break;
                    }

                    cout << "\nThere are " << marbles << " marbles remaining in the pile\n" << endl;

                    currentPlayer = player2;

                    turnDone = true;

                }else if (marbles == 1)
                {
                    marbles = marbles - selection;

                    if (marbles <= 0)
                    {
                        cout << "\nThere are 0 marbles remianing in the pile\n";

                        cout << "You drew the last marble!" << endl;
                        cout << "You lose!\n\n" << endl;
                        cout << player2 << " wins\n\n" << endl;

                        turnDone = true;

                        return 0;

                    }

                }else if (selection == 0 || selection > marbles / 2 + 1)
                {
                    cout << "\nYou must draw at least 1 marble, and no more than half the pile\n" << endl;
                }

            }else if (currentPlayer == player2)
            {
                cout << currentPlayer << "\n";

                cout << "How many marbles would you like to draw? : ";
                cin >> selection;

                if (selection < marbles / 2 +1 && marbles > 1 && selection != 0)
                {
                    marbles = marbles - selection;

                    if (marbles == 1)
                    {
                        cout << "\nUh Oh! There is only 1 marble remaining in the pile\n" << endl;

                        currentPlayer = player1;
                        turnDone = true;
                        break;
                    }

                    cout << "\nThere are " << marbles << " marbles remaining in the pile\n" << endl;

                    currentPlayer = player1;

                    turnDone = true;

                }else if (marbles == 1)
                {
                    marbles = marbles - selection;

                    if (marbles <= 0)
                    {
                        cout << "\nThere are 0 marbles remianing in the pile\n";

                        cout << "You drew the last marble!" << endl;
                        cout << "You lose!\n\n" << endl;
                        cout << player1 << " wins!\n\n" << endl;

                        turnDone = true;

                        return 0;

                    }

                }else if (selection == 0 || selection > marbles / 2 + 1)
                {
                    cout << "\nYou must draw at least 1 marble, and no more than half the pile\n" << endl;
                }
            }
        }
    }while (isDone == false);

    return 0;
}

以下是在程序中使用 std::cin.fail() 的方法:

int marbles;
bool check = false;
do {
    std::cout << "Enter how many marbles you would like to start with in the pile: ";
    std::cin >> marbles;
    if (std::cin.fail()) {
        std::cin.clear();
        std::cin.ignore();
    }
    else
        check = true;
}while(!check);

cin.fail() 將返回一個布爾值,說明是否收到了預期的收入
如果沒有,您將必須使用 clear() 將失敗值設置為 false 然后忽略給定的輸入
希望有所幫助

獲取用戶輸入的字符串並檢查其中是否有任何數字:

std::string input; 
std::cin >> input;
int marbles = std::atoi(input.c_str()); //Returns the first number met in the char table, if there is none, returns 0

if(marbles)
    std::cout << marbles;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM