Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单C-Tic-Tac-Toe输入错误_C_Input_Tic Tac Toe - Fatal编程技术网

简单C-Tic-Tac-Toe输入错误

简单C-Tic-Tac-Toe输入错误,c,input,tic-tac-toe,C,Input,Tic Tac Toe,我目前正在尝试学习C语言,并决定做一个简单的井字游戏 我不想做任何花哨的事情,我只想把用户的位置和他们给定的角色放到我的网格中。我还没有开始使用数组或任何东西,所以我使用printf构建了一个网格 问题是当我尝试实际运行游戏时,角色无法工作。我可以键入正确的输入,但这只会使该位置变为空白。如何使此switch语句将字符存储在所需的位置 #include <stdio.h> #include <stdlib.h> int main() {

我目前正在尝试学习C语言,并决定做一个简单的井字游戏

我不想做任何花哨的事情,我只想把用户的位置和他们给定的角色放到我的网格中。我还没有开始使用数组或任何东西,所以我使用printf构建了一个网格

问题是当我尝试实际运行游戏时,角色无法工作。我可以键入正确的输入,但这只会使该位置变为空白。如何使此switch语句将字符存储在所需的位置

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    // Initialize and define all variables and characters
    char c1='.', c2='.', c3='.', c4='.', c5='.', c6='.', c7='.', c8='.', c9='.'; // Board     characters
    char given; // User-input character
    int pos; // User-input position for the character
    int turnCount; // Gives a limit on game length

    for (turnCount = 0; turnCount < 9; turnCount++)
    {
        // Display lines
        printf("%c %c %c   1 2 3\n", c1, c2, c3); // First line
        printf("%c %c %c   4 5 6\n", c4, c5, c6); // Second line
        printf("%c %c %c   7 8 9\n", c7, c8, c9); // Third line

        // Asks for input, scans input for position and character to use
        printf("Choose a position and character, without spaces.\n");
        scanf_s("%i%c", &pos, &given);

        // Matches the input position with a character, defines the character
        switch (pos)
        {
        case 1: c1 = given; break;
        case 2: c2 = given; break;
        case 3: c3 = given; break;
        case 4: c4 = given; break;
        case 5: c5 = given; break;
        case 6: c6 = given; break;
        case 7: c7 = given; break;
        case 8: c8 = given; break;
        case 9: c9 = given; break;
        } // End switch
    } // End for - game should be over
    return 0;
}
#包括
#包括
int main()
{
//初始化并定义所有变量和字符
字符c1='、c2='、c3='、c4='、c5='、c6='、c7='、c8='、c9=';//电路板字符
char给定;//用户输入字符
int pos;//用户输入字符的位置
int turnCount;//对游戏长度进行限制
用于(营业额=0;营业额<9;营业额++)
{
//显示行
printf(“%c%c%c13\n”,c1,c2,c3);//第一行
printf(“%c%c%c456\n”,c4、c5、c6);//第二行
printf(“%c%c%c 7 8 9\n”,c7,c8,c9);//第三行
//请求输入,扫描输入以查找要使用的位置和字符
printf(“选择位置和字符,不带空格。\n”);
扫描(“%i%c”、&pos和给定);
//将输入位置与字符匹配,定义字符
开关(pos)
{
案例1:c1=给定;中断;
案例2:c2=给定;中断;
案例3:c3=给定;中断;
案例4:c4=给定;中断;
案例5:c5=给定;断裂;
案例6:c6=给定;中断;
案例7:c7=给定;中断;
案例8:c8=给定;中断;
案例9:c9=给定;中断;
}//结束开关
}//游戏应该结束了
返回0;
}
替换
扫描(“%i%c”、&pos,&given)带有
scanf(“%i%c”、&pos、&给定)将解决您的问题。这是因为scanf_s需要字符输入的缓冲区大小,而scanf不需要。另一种方法是包含缓冲区大小:
scanf_s(“%i%c”、&pos、&given,1)

我已经测试了这两个系统,它们在我的windows系统上工作

编辑: 只是为了好玩,我用数组实现了这个。这样比较干净

#include <stdio.h>
#include <stdlib.h>

int main()
{
    // Initialize and define all variables and characters
    char gameBoard[3][3];
    char given; // User-input character
    int pos; // User-input position for the character
    int turnCount; // Gives a limit on game length
    int i;
    //set the gameboard to all '.'
    for(i=0;i<9;i++)
    {
        gameBoard[i/3][i%3] = '.';
    }

    for (turnCount = 0; turnCount < 9; turnCount++)
    {
        // Display lines
        printf("%c %c %c   1 2 3\n", gameBoard[0][0], gameBoard[0][1], gameBoard[0][2]); // First line
        printf("%c %c %c   4 5 6\n", gameBoard[1][0], gameBoard[1][1], gameBoard[1][2]); // Second line
        printf("%c %c %c   7 8 9\n", gameBoard[2][0], gameBoard[2][1], gameBoard[2][2]); // Third line

        // Asks for input, scans input for position and character to use
        printf("Choose a position and character, without spaces.\n");
        scanf_s("%i%c", &pos, &given,1);

        // sets the character on the gameboard.
        gameBoard[(pos-1)/3][(pos-1)%3] = given;
    } // End for - game should be over
    return 0;
}
#包括
#包括
int main()
{
//初始化并定义所有变量和字符
char gameBoard[3][3];
char给定;//用户输入字符
int pos;//用户输入字符的位置
int turnCount;//对游戏长度进行限制
int i;
//将游戏板设置为“全部”。”

对于(i=0;i如果我将
scanf\u s
更改为
scanf
,此代码似乎在Linux上工作。我这里没有Windows系统来测试它,但我假设
scanf\u s
函数调用有问题

根据这里的说法:

所以也许你应该试试你的
scanf_s
电话:

scanf_s("%i%c",&pos, &given, 1);

我现在无法验证这一点,但是…

因为scanf\u s要求为字符声明缓冲区大小。仅供参考-我在windows上测试了这一点,它可以工作。我知道,但你应该在回答中提到这一点!:)检查
scanf\u s()的结果总是很好的。
scanf_s("%i%c",&pos, &given, 1);