【发布时间】:2015-07-02 12:02:36
【问题描述】:
#include <iostream>
#include <ctime>
#include <limits>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
int a, b;
int I, P;
unsigned int x;
unsigned int y;
int n, m;
unsigned int X, O;
int tictac[3][3] = {
{1, 1, 1},
{1, 1, 1} ,
{1, 1, 1} };
cout << "Player 1, enter X or O:" << endl;
cin >> a;
while (a == X);
{
cout << "Now, fill in the desired coordinated in a 3x3 square, a[x][y]" << endl;
cout << "Enter 'x' in [x]" << endl;
cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
cin >> x;
cout << "Enter 'y' in [y]" << endl;
cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
cin >> y;
tictac[x][y] == X;
}
}
我正在编写一个程序,让 2 个玩家在 3x3 网格上玩 tictactoe,而其余的“CIN”命令拒绝接受输入。
我尝试将“CIN”命令更改为:
getline (cin, x)
getline (cin, y)
尝试将变量从 (Unsigned int) 更改为 (Signed int),并使用 cin.ignore() 命令,但问题仍然存在。
【问题讨论】:
-
恐怕
a == X不是您所期望的那样。X未初始化,因此您很有可能永远不会进入 while 循环。 -
@OpenTheCSV 你编译时没有启用那么多警告,是吗?如果您只允许它,您的编译器可能会指出您的代码存在的一些问题。
-
@OpenTheCSV 为什么在你阅读
int时告诉用户输入一个字母?! -
@Biffen 好点,我将其固定为“Char X, O = 0;”而不仅仅是“Unsigned int X, O;”但它仍然坏了。另外,如何让我的编译器在启用大多数警告的情况下运行?
-
@OpenTheCSV 这将取决于编译器。当然,它会有某种文档。如果没有,那么谷歌。