scanf() 语句要求第一个输入然后跳过第二个,输出我的开关然后退出,我希望 scanf 要求两个并且不退出

问题描述 投票:0回答:0

scanf 正在输出,而不是要求输入,立即输出我的 switch 语句菜单和退出我的代码。我想让它要求两个输入,然后转到菜单,您可以在其中选择一个选项来玩游戏、说明等……

我已经尝试了所有方法,从将输入输入到它们自己的函数中到将其粘贴到开关语句之前,我对我正在做的这一切都束手无策。我还尝试制作整个菜单和东西案例 2,而 scanf() 是它自己的独立案例,这样一切都不会像现在这样挤在一起。

如果你向下滚动到 int main 下面,你会看到我想要的 scanf 和 printf

int main() {
    int player = 1, i, choice;
    char mark;
    choices = 1;

    printf("Enter first players initials: ");
    scanf("%s", &user1);
    
    printf("Enter second players initials: ");
    scanf("%s", &user2);
    //this is where i am asking for the input and it skips over the entire &user 2 input



    switch(choices)
    {
        case 1:
            system("cls");
            printf("\t\t\t1. PLAY\n");
            printf("\t\t\t2. INSTRUCTIONS\n");
            printf("\t\t\t3. STATISTICS\n");
            printf("\t\t\t4. EXIT\n\n");

            printf("\t\t\tENTER A NUMBER (1-4): ");
            scanf("%d", &def);

            if (def == 1) {

                    do {
                        system("cls");
                    displayBoard();
                    player = (player % 2) ? 1 : 2;

                    printf("Player %d, enter a number:  ", player);
                    scanf("%d", &choice);

                    mark = (player == 1) ? 'X' : 'O';

                    if (choice == 1 && square[1] == '1') {
                        square[1] = mark;
                    } else if (choice == 2 && square[2] == '2') {
                        square[2] = mark;
                    } else if (choice == 3 && square[3] == '3') {
                        square[3] = mark;
                    } else if (choice == 4 && square[4] == '4') {
                        square[4] = mark;
                    } else if (choice == 5 && square[5] == '5') {
                        square[5] = mark;
                    } else if (choice == 6 && square[6] == '6') {
                        square[6] = mark;
                    } else if (choice == 7 && square[7] == '7') {
                        square[7] = mark;
                    } else if (choice == 8 && square[8] == '8') {
                        square[8] = mark;
                    } else if (choice == 9 && square[9] == '9') {
                        square[9] = mark;
                    } else {
                        printf("Invalid move");
                        player--;
                        getch();
                    }
                    i = checkWin();

                    player++;
                }
                while (i ==  -1);

                displayBoard();

                if (i == 1)
                {
                    printf("==>\aPlayer %d win ", --player);
                }
                else
                {
                    printf("==>\aGame draw");
                }

                getch();
                return main();
            }

            if (def == 2) {
                system("cls");
                printf("----INSTRUCTIONS----\n\n");

                printf("To begin the game, select play in the menu.\n");
                printf("This is your board:\n\n");
                printf("----------\n");
                printf("1 | 2 | 3\n");
                printf("----------\n");
                printf("4 | 5 | 6\n");
                printf("----------\n");
                printf("7 | 8 | 9\n");
                printf("----------\n\n");
                printf("Players will take turns inputting their X or O, ('X' or 'O') by entering the number on the board.\n");
                printf("The first player to get three of their letter in a row wins.\n\n");

                return main();
            }

            if (def == 3) {
                system("cls");
                printf("COMING IN A FUTURE UPDATE!\n\n");
                printf("SORRY DAVID :(");
                return main();
                struct Player;
            }

            if (def == 4)
            {
                exit(0);
            }
    }
}
c switch-statement scanf
© www.soinside.com 2019 - 2024. All rights reserved.