密码验证程序

时间:2017-04-08 19:40:01

标签: c++ passwords c-strings

所以我尝试了许多不同的代码,并且不能让它运行在第一个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;

}

1 个答案:

答案 0 :(得分:0)

如果没有函数,则无法编写程序,因为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; } 是一个函数,并且您正在使用函数从用户获取输入。

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

import json
original_sentence = {
    'key': ['a', 'b', 'c'],
}
result = json.dumps(original_sentence)  # '{"key": ["a", "b", "c"]}'

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