我的switch语句中的默认值为循环

问题描述 投票:-1回答:1

你好,我是C ++的一个完整的初学者。我尝试制作井字游戏。但是在switch语句中,默认情况下检查无效的用户输入,运行时会弄乱它。默认情况下会循环播放。如果有人可以告诉我一种更有效的方法,那就太好了。该程序被卡在显示“无效输入”的位置。this is how the programm should look like

this is the error(it's stuck like this)

#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
//Declaring global variables
//the slot number the player has chosen
int slotch;
//the player number(which player's chance to play
int playc = 1;
//no of times moves has been made
int times = 0;
//opposite of the player number
int playcx;
//sign of the current player(X or O)
string sign;
//variables displayed on each slots
string slot1 = "slot1";
string slot2 = "slot2";
string slot3 = "slot3";
string slot4 = "slot4";
string slot5 = "slot5";
string slot6 = "slot6";
string slot7 = "slot7";
string slot8 = "slot8";
string slot9 = "slot9";

void display();
void play();
//main function
int main()
{
    display();
}
//display function for drawing on screen
void display()
{
    //to check the number of times the players made each move and to restart the game
    if (times == 9) {
        times = 0;
        system("cls");
        cout << "          |            |" << endl
             << "          |                        | " << endl;
        cout << "  " << slot1 << "   |   " << slot2 << "    |   " << slot3 << endl
             << "          |            |   " << endl
             << "_________ |                 __________            | __________ " << endl;
        cout << "          |            |" << endl
             << "          |                        | " << endl;
        cout << "  " << slot4 << "   |   " << slot5 << "    |   " << slot6 << endl
             << "          |            |   " << endl
             << "_________ |                 __________            | __________ " << endl;
        cout << "          |            |" << endl
             << "          |                        | " << endl;
        cout << "  " << slot7 << "   |   " << slot8 << "    |   " << slot9 << endl
             << "          |            |   " << endl
             << "          |                        | " << endl;
        system("pause");
        slot1 = "slot1";
        slot2 = "slot2";
        slot3 = "slot3";
        slot4 = "slot4";
        slot5 = "slot5";
        slot6 = "slot6";
        slot7 = "slot7";
        slot8 = "slot8";
        slot9 = "slot9";
        playc = 1;
    }
    //to draw the output
    system("cls");
    cout << "          |            |" << endl
         << "          |                    | " << endl;
    cout << "  " << slot1 << "   |   " << slot2 << "    |   " << slot3 << endl
         << "          |            |   " << endl
         << "_________ | __________ |             __________ " << endl;
    cout << "          |            |" << endl
         << "          |                    | " << endl;
    cout << "  " << slot4 << "   |   " << slot5 << "    |   " << slot6 << endl
         << "          |            |   " << endl
         << "_________ | __________ |             __________ " << endl;
    cout << "          |            |" << endl
         << "          |                    | " << endl;
    cout << "  " << slot7 << "   |   " << slot8 << "    |   " << slot9 << endl
         << "          |            |   " << endl
         << "          |            |                          " << endl;
    play();
}
//play function for gameplay
void play()
{
    //to decide which player has the chance
    if (playc == 1) {
        sign = "  X  ";
        playc = 2;
        playcx = 1;
    }
    else if (playc == 2) {
        sign = "  O  ";
        playc = 1;
        playcx = 2;
    }

    cout << "It's player" << sign << "'s chance." << endl
         << " Enter the number of the slot you want to put your " << sign << " in and press enter." << endl;
    //to get the slot no. from the player
    cin >> slotch;
    //to descide what is displayed in the display function
    switch (slotch) {
    case 1:
        if (slot1 == "slot1") {
            slot1 = sign;
        }
        else {
            playc = playcx;
            times--;
        }
        break;
    case 2:
        if (slot2 == "slot2") {
            slot2 = sign;
        }
        else {
            playc = playcx;
            times--;
        }
        break;
    case 3:
        if (slot3 == "slot3") {
            slot3 = sign;
        }
        else {
            playc = playcx;
            times--;
        }
        break;
    case 4:
        if (slot4 == "slot4") {
            slot4 = sign;
        }
        else {
            playc = playcx;
            times--;
        }
        break;
    case 5:
        if (slot5 == "slot5") {
            slot5 = sign;
        }
        else {
            playc = playcx;
            times--;
        }
        break;
    case 6:
        if (slot6 == "slot6") {
            slot6 = sign;
        }
        else {
            playc = playcx;
            times--;
        }
        break;
    case 7:
        if (slot7 == "slot7") {
            slot7 = sign;
        }
        else {
            playc = playcx;
            times--;
        }
        break;
    case 8:
        if (slot8 == "slot8") {
            slot8 = sign;
        }
        else {
            playc = playcx;
            times--;
        }
        break;
    case 9:
        if (slot9 == "slot9") {
            slot9 = sign;
        }
        else {
            playc = playcx;
            times--;
        }
        break;
    default:
        cout << "invalid input";
        playc = playcx;
        times--;
    }
    cin.clear();
    //adds one to the number of times players made their moves
    times++;
    display();
}
c++ switch-statement default
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.