【问题标题】:My CIN command skips input entirely? I've tried everything我的 CIN 命令完全跳过输入?我已经尝试了一切
【发布时间】: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 这将取决于编译器。当然,它会有某种文档。如果没有,那么谷歌。

标签: c++ input cin


【解决方案1】:
 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;

这是一个您丢弃其结果的比较。使用= 进行分配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    相关资源
    最近更新 更多