简体   繁体   English

错误] ISO C++ 禁止指针和整数之间的比较 [-fpermissive]

[英]Error] ISO C++ forbids comparison between pointer and integer [-fpermissive]

I'm a beginner in c++ programming and I have this activity in school.我是 C++ 编程的初学者,我在学校有这项活动。 I keep getting [Error] ISO C++ forbids comparison between pointer and integer [-fpermissive] in line 15. How do you solve this?我在第 15 行不断收到 [错误] ISO C++ 禁止比较指针和整数 [-fpermissive]。你是如何解决这个问题的? Thanks!谢谢!

#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();
}

You are doing things in a very strange way.你正在以一种非常奇怪的方式做事。 If you wanna compare int s.如果你想比较int s。 Take and int , read it and compare, Why is the array needed?取和int ,读取并比较,为什么需要array

The best and simple way to do this is to use only int s.最好和最简单的方法是只使用int s。

#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();
}

If you want to do it the way you want, then use strcmp()如果您想按照自己的方式进行操作,请使用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();
}

You are reading characters and comparing them as ints.您正在阅读字符并将它们作为整数进行比较。 That won't work....那行不通....

The following first places the charactes into an array of characters, then converts that aray to an int and compares the int:下面首先将字符放入字符数组,然后将该数组转换为 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();
}

pass is an array (which is implemented in c++ as a pointer) and 86222 is an integer. pass 是一个数组(在 C++ 中作为指针实现),而 86222 是一个整数。 you cannot compare those.你不能比较那些。

As @haris said in their comment, you really just want to store the input as an integer.正如@haris 在他们的评论中所说,您真的只想将输入存储为整数。 you do this with std::cin >> pass .你用std::cin >> pass做到这一点。 then you can compare pass with your stored value.然后您可以将pass与您的存储值进行比较。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ARDUINO:ISO C++ 禁止指针和整数之间的比较 [-fpermissive] - ARDUINO: ISO C++ forbids comparison between pointer and integer [-fpermissive] ISO C ++禁止比较指针和整数[-fpermissive] - ISO C++ forbids comparison between pointer and integer [-fpermissive] C ++错误:ISO C ++禁止指针和整数之间的比较[-fpermissive] - C++ Error: ISO C++ Forbids Comparison Between Pointer and Integer [-fpermissive] ISO C++ 禁止在 Arduino c 串行通信中比较指针和整数 [-fpermissive] 错误 - ISO C++ forbids comparison between pointer and integer [-fpermissive] error in Arduino c serial communication 得到错误:“ISO C ++禁止指针和整数之间的比较[-fpermissive]”如何修复? - Getting error: “ISO C++ forbids comparison between pointer and integer [-fpermissive]” How do I fix? Qt错误iso c ++禁止比较指针和整数-fpermissive - Qt error iso c++ forbids comparison between pointer and integer -fpermissive ISO C ++禁止在devc ++中比较指针和整数[-fpermissive]错误 - ISO C++ forbids comparison between pointer and integer [-fpermissive] error in devc++ 错误:ISO C++ 禁止在 Lambda ZC1C42145268E617A945D 中的指针和 integer [-fpermissive] 之间进行比较 - error: ISO C++ forbids comparison between pointer and integer [-fpermissive] in a Lambda function ISO C ++禁止在C ++代码中比较指针和整数[-fpermissive] - ISO C++ forbids comparison between pointer and integer[-fpermissive] in C++ code ISO C++ 禁止比较指针和整数 [-fpermissive]| [C++] - ISO C++ forbids comparison between pointer and integer [-fpermissive]| [c++]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM