我的CIN命令完全跳过输入?我尝试了一切

时间:2015-07-02 12:02:37

标签: c++ input cin

#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,其余的&#34; CIN&#34;命令拒绝接受输入。

我试过改变&#34; CIN&#34;命令:

getline (cin, x)
getline (cin, y)

尝试将变量从(Unsigned int)更改为(Signed int),并使用cin.ignore()命令,但问题仍然存在。

1 个答案:

答案 0 :(得分:2)

 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);

在最后一行中,X尚未初始化,因此您需要将a的值与特定值进行比较。此外,最后的分号使循环重复一个空语句。

      tictac[x][y] == X;

这是一个比较结果,你扔掉了。使用=进行分配。