C编程陷入错误循环中,带有IF-ELSE语句和strcmp

发布于 2025-02-13 18:28:09 字数 3416 浏览 0 评论 0原文

我遇到了一个错误,我不知道如何修复它。

此功能的本质是将用户的坐标放在选定的坐标中,然后将播放器的符号输入。我通过将输入作为char,使用strcmp将其与9个坐标空间之一进行比较,然后使用IF语句将其转换为整数。然后,它将将玩家的符号输入到这个空间中。但是,我想确保用户没有输入无效的坐标。问题在于,它一直在说输入无效。我尝试使用“ A1”调试。我检查了strCMP,结果为0。我检查了无效,结果为0。然后,我检查了row_move和column_move,他们被分配给[0] [2]的正确值。但是,最初,我确实注意到当我输入A1时,column_move值是极高的任意数字。有人可以帮我解决这个问题吗?

void Player1Move()
{       
        char p1move[2];
        int row_move, column_move, invalid;
        row_move = 0; 
        column_move = 0;
        invalid = 0; 
        printf("It's Player 1's turn!\n");
        printf("What's your move?\n");
        int rescheck;
        do
        {       
                printf("Enter your move ");
                scanf("%s", p1move); 
                printf("YOUR MOVE IS %s\n", p1move);
                printf("Check Invalid: %d\n", invalid);
                rescheck = strcmp(p1move,"A1"); 
                printf("Check String A1: %d\n", rescheck);
                printf("Row: %d\n", row_move);
                printf("Column: %d\n", column_move);
                if(strcmp(p1move,"A1") == 0)
                {       
                        row_move = 2; 
                        column_move = 0; 
                } else if(strcmp(p1move, "A2") == 0)
                {       
                        row_move = 1; 
                        column_move = 0; 
                } else if(strcmp(p1move, "A3") == 0)
                {       
                        row_move = 0; 
                        column_move = 0; 
                } else if(strcmp(p1move, "B1") == 0)
                {       
                        row_move = 2; 
                        column_move = 1; 
                } else if(strcmp(p1move, "B2") == 0)
                {       
                        row_move = 1; 
                        column_move = 1; 
                } else if(strcmp(p1move, "B3") == 0)
                {       
                        row_move = 0; 
                        column_move = 1; 
                } else if(strcmp(p1move, "C1") == 0)
                {       
                        row_move = 2; 
                        column_move = 2; 
                } else if(strcmp(p1move, "C2") == 0)
                {       
                        row_move = 1; 
                        column_move = 2; 
                } else if(strcmp(p1move, "C3") == 0)
                {       
                        row_move = 1; 
                        column_move = 2;
                } else
                {       
                        invalid = 1;
                }
                
                /* check if board space is available */
                
                if(invalid == 1 || board[row_move][column_move] != ' ')
                {       
                        printf("That is not a valid space, please pick a new space\n");
                } else
                {       
                        board[row_move][column_move] = PLAYER1;
                        break;
                }
        }       while(board[row_move][column_move] != ' ');
}

编辑2:

我编辑了该功能,但仍会产生相同的错误。

It's Player 1's turn!
What's your move?
Enter your move A1
Move is [0][4196901]
Check Invalid: 0
String: A1
That is not a valid space, please pick a new space
Enter your move 

这是我最初放入A1时会发生的情况,我仍会遇到此错误。我更改了炭尺寸并更新了SCANF。我检查了是否受到影响,例如行和列的选择以及无效的检查。如下所示,column_move似乎存在错误,并且仍然是一个任意号码。

I'm running into an error and I do not know how to fix it.

The essence of this function is to take a user's coordinate and input the player's symbol into the chosen coordinate. I did this by taking the input as a char, using strcmp to compare it to one of the 9 coordinate spaces, and then converting it into an integer using if statements. It would then input the player's symbol into this space. However, I wanted to make sure that the user is not inputting invalid coordinates. The problem is it keeps saying the input is invalid. I tried debugging using "A1". I checked strcmp and it resulted in 0. I checked invalid and it resulted in 0. I then checked row_move and column_move and they're getting assigned to the correct value of [0][2]. However, I did notice initially when I input A1, the column_move value is extremely high arbitrary number. Can someone help me figure this out?

void Player1Move()
{       
        char p1move[2];
        int row_move, column_move, invalid;
        row_move = 0; 
        column_move = 0;
        invalid = 0; 
        printf("It's Player 1's turn!\n");
        printf("What's your move?\n");
        int rescheck;
        do
        {       
                printf("Enter your move ");
                scanf("%s", p1move); 
                printf("YOUR MOVE IS %s\n", p1move);
                printf("Check Invalid: %d\n", invalid);
                rescheck = strcmp(p1move,"A1"); 
                printf("Check String A1: %d\n", rescheck);
                printf("Row: %d\n", row_move);
                printf("Column: %d\n", column_move);
                if(strcmp(p1move,"A1") == 0)
                {       
                        row_move = 2; 
                        column_move = 0; 
                } else if(strcmp(p1move, "A2") == 0)
                {       
                        row_move = 1; 
                        column_move = 0; 
                } else if(strcmp(p1move, "A3") == 0)
                {       
                        row_move = 0; 
                        column_move = 0; 
                } else if(strcmp(p1move, "B1") == 0)
                {       
                        row_move = 2; 
                        column_move = 1; 
                } else if(strcmp(p1move, "B2") == 0)
                {       
                        row_move = 1; 
                        column_move = 1; 
                } else if(strcmp(p1move, "B3") == 0)
                {       
                        row_move = 0; 
                        column_move = 1; 
                } else if(strcmp(p1move, "C1") == 0)
                {       
                        row_move = 2; 
                        column_move = 2; 
                } else if(strcmp(p1move, "C2") == 0)
                {       
                        row_move = 1; 
                        column_move = 2; 
                } else if(strcmp(p1move, "C3") == 0)
                {       
                        row_move = 1; 
                        column_move = 2;
                } else
                {       
                        invalid = 1;
                }
                
                /* check if board space is available */
                
                if(invalid == 1 || board[row_move][column_move] != ' ')
                {       
                        printf("That is not a valid space, please pick a new space\n");
                } else
                {       
                        board[row_move][column_move] = PLAYER1;
                        break;
                }
        }       while(board[row_move][column_move] != ' ');
}

Edit 2:

I edited the function and still producing the same error.

It's Player 1's turn!
What's your move?
Enter your move A1
Move is [0][4196901]
Check Invalid: 0
String: A1
That is not a valid space, please pick a new space
Enter your move 

This is what happens when I initially put in A1, I'm still getting this error. I had changed the char size and updated the scanf. I had checked to see if anything is being affected such as the row and column choices along with the invalid check. As shown here, there seems to be an error with column_move and it's still an arbitrary number.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对岸观火 2025-02-20 18:28:09

您声明了一个具有两个元素的字符数组,

char p1move[2];

因此您可以输入一个字符串,该字符串仅包含一个字符,除了终止零字符'\ 0''

但是,您正在尝试将数组的内容与包含三个元素(包括终止零字符'\ 0')的字符串进行比较:例如

if(strcmp(p1move,"A1") == 0)

注意:字符串文字“ a1” < /code>存储为具有三个字符的字符数组{'a','1','\ 0'}

如果您尝试输入两个字符,那么该行为将不确定,因为您将覆盖数组之外的内存。

声明数组至少具有三个元素

char p1move[3];

,然后重写scanf的呼叫

scanf("%2s", p1move);

You declared a character array with two elements

char p1move[2];

So you may enter a string that contains only one character apart from the terminating zero character '\0'.

However you are trying to compare the content of the array with strings that contain three elements (including the terminating zero character '\0') as for example

if(strcmp(p1move,"A1") == 0)

Note: the string literal "A1" is stored as a character array with three characters { 'A', '1', '\0' }.

If you will try to enter two characters then the behavior will be undefined because you will overwrite memory beyond the array.

Declare the array as having at least three elements

char p1move[3];

and rewrite the call of scanf like

scanf("%2s", p1move);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文