密码验证程序

Password verifier

本文关键字:程序 验证 密码      更新时间:2023-10-16

所以我尝试了许多不同的方法,但无法让它运行第一个if语句,也无法打印出每个问题。 我的问题是是否可以在不使用函数的情况下做到这一点?如果是这样,请您帮助我修复我的代码,以便它正常工作或至少引导我朝着正确的方向前进。我觉得这段代码缺少一两个关键部分,我无法理解它。

******更新

版本****

(仍然行不通(

#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
using namespace std;
//Create program that verifies that a password meets certain criteria.The 
//password must :
//-Be at least eight characters long
//- Contain an uppercase letter
//- Contain a lowercase letter
//- Contain a numeric digit
//Prompt the user to enter a password and then verify it.Your program should 
//list every issue the password has.
int main()
{
cout << "**************************************************** n";
cout << "Your password must be characters long and containt: n";
cout << "An uppercase letter. n";
cout << "A lowercase letter. n";
cout << "And a number. n";
cout << "**************************************************** n";
cout << "    n";
const int size = 200;
char pass1[size], pass2[size];
const unsigned int password_length = 8U;
unsigned int length = 0;
bool has_lower_case_letter = false;
bool has_digit = false;
bool has_upper_case_letter = false;
char password[password_length + 1];
    cout << "Please enter a password: ";
        cin >> pass1;
while (length < password_length)
{
    if (!has_lower_case_letter)
    {
        static const char lower_case_letters[] = "abcdefghijklmnopqrstuvwxyz";
        static const int letter_quantity = 
            sizeof(lower_case_letters) / sizeof(lower_case_letters[0]);
        for (int i = 0; i < letter_quantity; ++i)
        {
            if (pass1[size] == lower_case_letters[i])
            {
                has_lower_case_letter = true;
                break;
            }
        }
    }
    if (!has_upper_case_letter)
    {
        static const char upper_case_letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        static const int letter_quantity =
            sizeof(upper_case_letters) / sizeof(upper_case_letters[0]);
        for (int i = 0; i < letter_quantity; ++i)
        {
            if (pass1[size] == upper_case_letters[i])
            {
                has_upper_case_letter = true;
            }
        }
    }
    if (!has_upper_case_letter)
    {
        static const char digits[] = "0123456789";
        static const int digit_quantity =
            sizeof(digits) / sizeof(digits[0]);
        for (int i = 0; i < digit_quantity; ++i)
        {
            if (pass1[size] == digits[i])
            {
                has_digit = true;
            }
        }
    }
    password[length] = pass1[size];
    ++length;
}
if (!has_digit)
    cout << "The password must contain a digit. n";
if (!has_upper_case_letter)
    cout << "The password must include an uppercase letter. n";
if (!has_lower_case_letter)
    cout << "The password must include a lowercase letter. n";
return 0;

}

你不能

在没有函数的情况下编写程序,因为main是一个函数,你正在使用一个函数来获取用户的输入。

也就是说,是的,您可以在不使用搜索函数的情况下检查文本字符串。

const unsigned int password_length = 8U;
unsigned int length = 0;
bool has_lower_case_letter = false;
bool has_digit = false;
bool has_upper_case_letter = false;
char password[password_length + 1];
while (length < password_length)
{
  char c;
  cin >> c;
  if (!has_lower_case_letter)
  {
    static const char lower_case_letters[] = "abcdefghijklmnopqrstuvwxyz"
    static const letter_quantity =
        sizeof(lower_case_letters) / sizeof(lower_case_letters[0]);
    for (unsigned int i = 0; i < letter_quantity; ++i)
    {
       if (c == lower_case_letters[i])
       {
          has_lower_case_letter = true;
          break;
       }
    }
  }
  //...
  password[length] = c;
  ++length;
}

大写字母和数字的检查留给海报作为练习。