【问题标题】:error: ISO C++ forbids comparison between pointer and integer [-fpermissive]| [closed]错误:ISO C++ 禁止指针和整数之间的比较 [-fpermissive]| [关闭]
【发布时间】:2020-01-07 18:06:53
【问题描述】:

这是验证密码的代码。正在检查输入字符串的数字、特殊字符、字符串长度以及大小写字符。 将字符串元素与特殊字符进行比较的特定 for 循环块会给出此错误。我尝试将这些特殊字符初始化为 char 变量以进行比较,但错误仍然存​​在。

char k='@';
char n='#';
char m='$';
for(i=0; i<l; i++)
        {
            if(password[i]==k || password[i]==n || password==m)     
            {
                counts++;
            }
        }

我尝试将它们与此进行比较,但同样的错误

 if(password[i]=='@' || password[i]=='#' || password=='$')     
            {
                counts++;
            }

这是代码

    #include <iostream>
    #include<string.h>

    using namespace std;

    int main()
    {
     char password[20];
     char k='@';
     char n='#';
     char m='$';
     int i,j,l,countd=0,counts=0,countl=0,countu=0;
     cout<<"enter a password with following conditions :\n1. There should be atleast one digit. \n2. 
 there should be atleast one of #, @, $";
    cout<<"3. password should be between 6 to 20 characters \n4.there should be more uppercase 
letters than lower case\n";
cout<<"5. Should start with uppercase and end with lowercase";

do
{
    countd=0,counts=0,countl=0,countu=0;
    cout<<endl<<"ENTER PASSWORD :";
    cin.getline(password,20);
    l=strlen(password);
    if(l>20 || l<6)
    {
        cout<<"password length should be between 6 to 20 characters \n";
    }
    if(islower(password[0]))
    {
        cout<<" first letter should be uppercase \n";
    }
    if(isupper(password[l-1]))
    {
        cout<<" last letter should be lowercase \n";
    }


    for(i=0; i<l; i++)
    {
        for(j='0'; j<='9'; j++)
        {
            if(password[i]==j)
            {
                countd++;

            }

        }
    }
    if(countd<1)
    {
        cout<<" password should contain atleast one digit \n";
    }

    for(i=0; i<l; i++)
    {
        if(password[i]==k || password[i]==n || password==m)     //char k='@'; char n='#'; char m='$'; |56|error: ISO C++ forbids comparison between pointer and integer [-fpermissive]|
        {
            counts++;
        }
    }


    if(counts<1)
    {
        cout<<"password should contain atleast one of the three special character \n";
    }


    for(i=0; i<l; i++)
    {
        for(j='A'; j<='Z'; j++)
        {
            if(password[i]==j)
            {
                countu++;
            }
        }
    }
    for(i=0; i<l; i++)
    {
        for(j='a'; j<='z'; j++)
        {
            if(password[i]==j)
            {
                countl++;
            }
        }
    }
    if(countu<countl)
    {
        cout<<"number of uppercase letters should be more than lowercase \n";
    }

}

while((countu<countl) || (counts<1) (isupper(password[l-1])) || (islower(password[0])) || l>20 || l<6 || (countd<1));

cout<<endl<<"password accepted/validated "<<endl;


return 0;
}

最小的可重现示例。 只是想要一种将输入字符串与特殊字符进行比较的方法。

char password[20];
char k='@';
char n='#';
char m='$';
for(i=0; i<l; i++)
        {
            if(password[i]==k || password[i]==n || password==m)   
            {
                counts++;
            }
        }

我怎样才能进行这种比较? 请指导。

【问题讨论】:

  • 如果你幸运的话,你的编译器还应该给你一个列指针,它会告诉你忘记了三个索引括号中的哪一个!
  • 欢迎堆栈溢出!不幸的是,这个问题不够详细,无法为您提供任何有意义的帮助。请编辑您的问题,以包含该问题的最小可重现示例,包括示例输入、首选输出和您迄今为止尝试过的代码。
  • 为什么期望能够有意义地比较指针和整数??
  • 感谢大家的回复。问题出在索引括号和(counts&lt;1) (isupper(password[l-1])) 现在已更正为(counts&lt;1) ||(isupper(password[l-1])) 程序正在运行。再次感谢!!

标签: c++ char integer


【解决方案1】:

这一行

if(password[i]==k || password[i]==n || password==m) 

应该阅读

if(password[i]==k || password[i]==n || password[i]==m) 

请注意最后一个学期缺少的括号。

【讨论】:

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