【问题标题】:Error] ISO C++ forbids comparison between pointer and integer [-fpermissive]错误] ISO C++ 禁止指针和整数之间的比较 [-fpermissive]
【发布时间】:2015-11-18 14:38:12
【问题描述】:

我是 C++ 编程的初学者,我在学校有这个活动。我不断收到 [Error] ISO C++ forbids comparison between pointer and integer [-fpermissive] 在第 15 行。你如何解决这个问题?谢谢!

#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
int pass[5];
int x;
main()
{
    cout<<"\nEnter pin code: ";
    for(x=0;x<=4;x++)
    {
        pass[x]=getch();
        putch('#');
    }   
    if(pass==86222)
        cout<<"\nW E L C O M E!";
    else
        cout<<"\nIncorrect Pin Code";
    getch();
}

【问题讨论】:

  • 你做事的方式很奇怪。如果你想比较ints。拿和int,阅读并比较,为什么需要array
  • 发布了一个简单的方法来做你正在尝试的事情。
  • @Haris 我们需要用# 或类似输入密码的方式隐藏密码。

标签: c++ c pointers integer


【解决方案1】:

你做事的方式很奇怪。如果你想比较ints。拿和int,阅读并比较,为什么需要array

最好和最简单的方法是只使用ints。

#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
main()
{
    int pass;
    cout<<"\nEnter pin code: ";
    cin>>pass;

    if(pass==86222)
        cout<<"\nW E L C O M E!";
    else
        cout<<"\nIncorrect Pin Code";
    getch();
}

如果您想按照自己的方式进行操作,请使用strcmp()

#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
char pass[5];
int x;
main()
{
    cout<<"\nEnter pin code: ";
    for(x=0;x<=4;x++)
    {
        pass[x]=getch();
        putch('#');
    }   
    if(!strcmp(pass, "86222"))
        cout<<"\nW E L C O M E!";
    else
        cout<<"\nIncorrect Pin Code";
    getch();
}

【讨论】:

  • 我们需要用#或者类似输入密码的东西隐藏pin码。 @哈里斯
  • 它说 [Error] cannot convert 'int*' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'
  • @hdlsswrrr,对不起,错了..应该是char pass[5]
【解决方案2】:

您正在读取字符并将它们作为整数进行比较。那不行……

下面首先将字符放入一个字符数组中,然后将该数组转换为 int 并比较 int:

char passch[6];
int pass, x;
main()
{
    cout<<"\nEnter pin code: ";
    for(x=0;x<=4;x++)
    {
        passch[x]=getch();
        putch('#');
    } 
    passch[5]= '\0';
    pass= atoi(passch);
    if(pass==86222)
        cout<<"\nW E L C O M E!";
    else
        cout<<"\nIncorrect Pin Code";
    getch();
}

【讨论】:

  • 这对我来说更像是一条评论。
  • 我能做些什么来解决这个问题?
【解决方案3】:

pass 是一个数组(在 c++ 中作为指针实现),86222 是一个整数。你不能比较那些。

正如@haris 在他们的评论中所说,您真的只想将输入存储为整数。你用std::cin &gt;&gt; pass 来做这件事。然后您可以将pass 与您的存储值进行比较。

【讨论】:

    猜你喜欢
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多