输入后转换cin(convert cin after input)

编程入门 行业动态 更新时间:2024-10-26 22:18:16
输入后转换cin(convert cin after input)

我正在尝试使用矩阵创建一个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'; } }

更多推荐

本文发布于:2023-08-07 15:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464767.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:cin   convert   input

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!