[英]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.