我正在尝试使用矩阵创建一个tic tac toe游戏,如果我将每个位置的用户输入为[0] [0]或[1] [2]等,它运行正常。但是,我想要用户能够输入“a,b,c”和“1,2,3”,但当我尝试更改输入值并在控制台中输入一个字母时,我得到:
ConsoleApplication3.exe中0x01294C54处的未处理异常:0xC0000005:访问冲突读取位置0x345D4330。
有没有办法在不发生这种情况的情况下更改用户的输入值?
这是代码的第一部分,问题出在Input函数中。
#include <iostream> #include <ctime> using namespace std; char matrix[3][3] = { '-', '-', '-', '-', '-', '-', '-', '-', '-' }; void Draw() { cout << " a b c" << endl; int row=1; for (int i = 0; i < 3; i++) { cout << row << " "; for (int j = 0; j < 3; j++) { cout << matrix[i][j] << " "; } cout <<endl; row++; } } void Input() { int pos1, pos2; cout << "Pick a place to put your X" << endl; cin >> pos1 >> pos2; if (pos1 == 'a') pos1 = '0'; if (pos1 == 'b') pos1 = '1'; if (pos1 == 'c') pos1 = '2'; if (pos2 == '1') pos2 = '0'; if (pos2 == '2') pos2 = '1'; if (pos2 == '3') pos2 = '2'; if (matrix[pos1][pos2] != 'O' && matrix[pos1][pos2] != 'X') { matrix[pos1][pos2] = 'X'; } }I'm trying to create a tic tac toe game using a matrix, and it runs fine if I have the user input each position as [0][0] or [1][2], etc. However, I want the user to be able to input "a, b, c" and "1, 2, 3", but when I try to change input values and enter a letter into the console, I get:
Unhandled exception at 0x01294C54 in ConsoleApplication3.exe: 0xC0000005: Access violation reading location 0x345D4330.
Is there a way to change the user's input values without this happening?
Here's the first part of the code, and the problem is in the Input function.
#include <iostream> #include <ctime> using namespace std; char matrix[3][3] = { '-', '-', '-', '-', '-', '-', '-', '-', '-' }; void Draw() { cout << " a b c" << endl; int row=1; for (int i = 0; i < 3; i++) { cout << row << " "; for (int j = 0; j < 3; j++) { cout << matrix[i][j] << " "; } cout <<endl; row++; } } void Input() { int pos1, pos2; cout << "Pick a place to put your X" << endl; cin >> pos1 >> pos2; if (pos1 == 'a') pos1 = '0'; if (pos1 == 'b') pos1 = '1'; if (pos1 == 'c') pos1 = '2'; if (pos2 == '1') pos2 = '0'; if (pos2 == '2') pos2 = '1'; if (pos2 == '3') pos2 = '2'; if (matrix[pos1][pos2] != 'O' && matrix[pos1][pos2] != 'X') { matrix[pos1][pos2] = 'X'; } }最满意答案
您正在使用越界索引访问matrix 。 访问matrix的有效索引是matrix[0][0]到matrix[2][2] 。 您正在使用字符'0' , '1'和'2'来访问矩阵。 如果您的系统使用ASCII编码,那么它们的数值是48,49和50。
此外,当您的变量是int类型并且您使用:
cin >> pos1;程序无法正确读取输入a 。 你需要使用:
char pos1; cin >> pos1;接受a作为有效输入。
这是您的功能的修改版本:
void Input() { char c1, c2; int pos1, pos2; cout << "Pick a place to put your X" << endl; cin >> c1 >> c2; if (c1 == 'a') pos1 = 0; else if (c1 == 'b') pos1 = 1; else if (c1 == 'c') pos1 = 2; else { // Error do something } if (c2 == '1') pos2 = 0; else if (c2 == '2') pos2 = 1; else if (c2 == '3') pos2 = 2; else { // Error do something } if (matrix[pos1][pos2] != 'O' && matrix[pos1][pos2] != 'X') { matrix[pos1][pos2] = 'X'; } }You are accessing matrix using out of bounds indices. The valid indices to access matrix are matrix[0][0] to matrix[2][2]. You are using the character '0', '1', and '2' to access the matrix. The numerical value of those, if your system uses ASCII encoding, are 48, 49, and 50.
Also, when your variable is of type int and you use:
cin >> pos1;the program will not read correctly the input a. You need to use:
char pos1; cin >> pos1;to accept a as a valid input.
Here's a modified version of your function:
void Input() { char c1, c2; int pos1, pos2; cout << "Pick a place to put your X" << endl; cin >> c1 >> c2; if (c1 == 'a') pos1 = 0; else if (c1 == 'b') pos1 = 1; else if (c1 == 'c') pos1 = 2; else { // Error do something } if (c2 == '1') pos2 = 0; else if (c2 == '2') pos2 = 1; else if (c2 == '3') pos2 = 2; else { // Error do something } if (matrix[pos1][pos2] != 'O' && matrix[pos1][pos2] != 'X') { matrix[pos1][pos2] = 'X'; } }更多推荐
发布评论