【问题标题】:Cannot compare pointer and integer c++无法比较指针和整数c ++
【发布时间】:2016-02-24 02:52:59
【问题描述】:

您好,我正在尝试编写一个简单的井字游戏,并且每当我编译它时都会显示:

error: ISO C== forbids comparison between pointer and integer
[-fpermissive]  } while(input != "Quit");
                                 ^

这是我的代码:

#include <iostream>
using namespace std;

/*
Board;
 1 | 2 | 3
---|---|---
 4 | 5 | 6
---|---|---
 7 | 8 | 9
*/

char square[9] = {'1','2','3','4','5','6','7','8','9'};

char input;

void board();

main()
{
    do
    {
        board();
        switch (input)          
        {
            case 1:
                square[1] = 'X';
            case 2:
                square[2] = 'X';
            case 3:
                square[3] = 'X';
            case 4:
                square[4] = 'X';
            case 5:
                square[5] = 'X';
            case 6:
                square[6] = 'X';
            case 7:
                square[7] = 'X';
            case 8:
                square[8] = 'X';
            case 9:
                square[9] = 'X';
            default:
                cout << "Invalid Input";
        }
    } while(input != "Quit");     //Here is where it is an error

    if(input == "Quit")
    {
        return 1;
    }

    cout << "\n\n"; 

    return 0;
}

void board()                //Builds the board
{
    cout << "\n\n\tTicTacToe\n\n";

    cout<<" "<<square[0]<<" | "<<square[1]<<" | "<<square[2]<< endl;
    cout << "---|---|---" << endl;

    cout<<" "<<square[3]<<" | "<<square[4]<<" | "<<square[5]<< endl;
    cout << "---|---|---" << endl;

    cout<<" "<<square[6]<<" | "<<square[7]<<" | "<<square[8]<< endl;

    cout << "Player 1 Make a Move:  ";
    cin.get();
    cin >> input;
}

仅供参考,这并不接近完整的游戏,我只是想弄清楚我将如何编写游戏的某些部分。

【问题讨论】:

  • inputchar 类型,而 "Quit"const char * 类型。您不能将字符与字符串(字符数组)进行比较。也许你正在寻找while(input == 'Q')
  • 如何使“退出”类型为 const char* ?
  • "Quit" 已经是 const char* 类型,但您需要 char 才能与 input 进行比较,请尝试使用 while(input == 'Q')
  • 为什么问题以 strong text 结尾?
  • 另外,考虑使用 std::string

标签: c++ pointers char


【解决方案1】:

问题在于变量input 被声明为单个char 数据类型,而您将它与字符串进行比较,或者在本例中为const char*,本质上是一个字符数组。由于"Quit" 是一个字符数组,因此您不能将其等同于单个char

【讨论】:

  • const char * 在他的情况下会出错,因为输入变量不能是 const。
  • 如果你知道我在说什么......他不能将它声明为 const
  • @FlareCat 我只说它的 const 是因为它是一个不变的、开发人员定义的项目,用于不变的比较。
  • 哦,我明白你在说什么了。
【解决方案2】:

在 if 中,您将 charchar * 进行比较。

将 if 中的值设置为

'Q'

或者使用

声明输入变量
char *input;

考虑查看字符串标题,它包含在这种情况下非常有用的功能。

编辑:您的代码的另一个错误:在您的开关情况下,您没有在每个case : 之后包含break;。这将导致正确大小写之后的所有内容都变为'X'

编辑 2:您不应该使用全局变量。只需通过引用传递它们即可。

编辑 3:main() 应该有一个类型。

【讨论】:

    【解决方案3】:

    在您的switch 语句中,您正在访问不存在的square[9]。这称为 缓冲区溢出,您可能正在覆盖其他变量或代码。

    使用您的board() 函数进行检查,该函数可以正确访问数组。

    【讨论】:

    • 这是真的;他的索引从 0 开始。但是,问题在于他正在与无法比较的值类型进行比较,char *char
    【解决方案4】:

    当您尝试分配或比较两个变量时,左运算符类型必须与左运算符类型相同,在您的情况下,左运算符是输入,char 类型的变量和左变量类型“Quit”的type const char[4],两种类型不同,无法比较!

    【讨论】:

    • 输入不是 int 类型!这是一个字符
    【解决方案5】:

    第一个是char,不能和char*比较,char*是一个char指针。
    请使用作为对象的std::string(或在这种情况下为string)而不是作为原始类型的char*

    会是这样的:

    string input;
    

    【讨论】:

      猜你喜欢
      • 2020-05-10
      • 2012-07-01
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多