为什么小写字母验证总是失败?

问题描述 投票:0回答:1

我正在编写一个程序,该程序将密码作为输入并根据以下标准进行验证:

  • 8个字符或更多
  • 至少一个大写字母
  • 至少一个小写字母
  • 至少一位数字

即使方法相同,验证功能也会成功检查除小写字母之外的所有条件。

附注我知道使用命名空间标准被认为是不好的做法,但就这个程序而言,据我所知,它不应该引起任何问题。

主要:

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

using namespace std;

int main()
{
    string password;    //string for user input
    bool longEnough, foundUpper, foundLower, foundDigit;    //booleans to validate password

    for(int i = 3; i > 0; i--)  //loop 3 times or until valid password is entered
    {
        longEnough = false; //reset bools to false
        foundUpper = false;
        foundLower = false;
        foundDigit = false;
        getInput(password); //calling getInput function
        if(validatePass(password, longEnough, foundUpper, foundLower, foundDigit) == true)  //check if the password is valid
        {
            getValidOutput(password); //calls function to display output for valid password
            exit(0);    //end program
        }
        else
        {
            getInvalidOutput(password, longEnough, foundUpper, foundLower, foundDigit);   //calls function to display output for invalid password
            cout << "You have " << i-1 << " more attempts.\n";
        }
    }
    cout << "You have run out of attempts, the session has been locked.";
    exit(0);    //end program
}

头文件:

#ifndef PASSWORDS_H_INCLUDED
#define PASSWORDS_H_INCLUDED

using namespace std;

void getInput(string& password) //prompts and receives user input for password
{
    cout << "Please enter a password with at least 8 characters, one uppercase, one lowercase, and one digit: ";
    cin >> password;
}

bool validatePass(string password, bool& longEnough, bool& foundUpper, bool& foundLower, bool& foundDigit)  //checks if password is valid
{
    if(password.length() >= 8)  //check for length
    {
        longEnough = true;
    }
    for(int i = 0; i < password.length(); i++)  //check each character
    {
        char position = password.at(i); //set current character
        if(isupper(position) == true)   //check for uppercase letters
        {
            foundUpper = true;
        }
        if(islower(position) == true)   //check for lowercase letters
        {
            foundLower = true;
        }
        if(isdigit(position) == true)   //check for digits
        {
            foundDigit = true;
        }
    }
    if(longEnough == true && foundUpper == true && foundLower == true && foundDigit == true)    //return true if valid, return false otherwise
    {
        return true;
    }
    else
    {
        return false;
    }
}

void getValidOutput(string password)    //output if the password is valid
{
    cout << "The password \"" << password << "\" is valid.";
}

void getInvalidOutput(string password, bool longEnough, bool foundUpper, bool foundLower, bool foundDigit)  //output if the password is invalid
{
    cout << "The password \"" << password << "\" is invalid for the following reasons:\n";
    if(longEnough == false)
    {
        cout << "The password has less than 8 characters\n";
    }
    if(foundUpper == false)
    {
        cout << "The password does not contain an uppercase letter\n";
    }
    if(foundLower == false)
    {
        cout << "The password does not contain a lowercase letter\n";
    }
    if(foundDigit == false)
    {
        cout << "The password does not contain a digit\n";
    }
}

#endif // PASSWORDS_H_INCLUDED

c++ char lowercase
1个回答
0
投票

您的支票有误:

    if(isupper(position) == true)   //check for uppercase letters
    {
        foundUpper = true;
    }

std::isupper

int isupper( int ch );

检查给定字符是否为当前安装的 C 语言环境分类的大写字符。在默认的“C”语言环境中,std::isupper 仅针对大写字母返回非零值 (ABCDEFGHIJKLMNOPQRSTUVWXYZ)。

它返回大写字母的非零值。它不会返回

true
。任何非零
int
都可以隐式转换为
bool
并产生
true
。不过,
2 == true
是假的,因为
true
被提升为
int
并产生
1
,而
2 == 1
false

将比较更改为

     if(isupper(position))
     {
         foundUpper = true;
     }
© www.soinside.com 2019 - 2024. All rights reserved.