我已经在这个问题上工作了无数个小时,但我找不到问题所在。我改变并测试了问题的每个部分,总是得到奇怪和错误的结果。我开始认为我的编译器可能出现故障。
这就是我想要做的:开发一个提示输入密码的程序,该程序检查是否满足以下条件。
最少 6 个字符长。
至少包含一个大写字母。
至少包含一个小写字母。
至少包含一位数字。
如果输入的密码不符合标准,则程序应显示原因并提示重新输入。如果密码正确,则会显示一条消息并结束程序。请帮忙!
注意:这是一个控制台 32 程序。
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <cctype>
#include "ctype.h"
using namespace std;
// Function prototype
bool lengthTest(char *);
bool lowCaseTest(char []);
bool upCaseTest(char []);
bool digitTest(char []);
const int SIZE = 20;
int main()
{
// Buffer to hold the string.
char password[SIZE];
int sumofbool;
// Program Intro Display
cout << "----PASSWORD VERIFIER PROGRAM----\n\n";
cout << "Enter a password that meets the following criteria:\n"
<< "-Minimum of 6 characters in length.\n"
<< "-Contains at least one uppercase and one lowercase letter.\n"
<< "-Contains at least one digit.\n\n";
cout << "->";
// Get input from user.
cin.getline(password, SIZE);
sumofbool = lengthTest(password) + lowCaseTest(password) + upCaseTest(password)
+ digitTest(password);
// if 1 or more of the 4 functions is not true, display why and prompt for re-entry.
while (sumofbool < 4)
{
if (!lengthTest(password))
cout << "Error, password must be at least 6 characters long.\n";
if (!upCaseTest(password))
cout << "Error, password must contain at least one upper case letter.\n";
if (!lowCaseTest(password))
cout << "Error, password must contain at least one lower case letter.\n";
if (!digitTest(password))
cout << "Error, password must contain at least one digit.\n";
cout << "Please re-enter password: ";
// prompt for re-entry and call functions to test input.
cin.getline(password, SIZE);
sumofbool = lengthTest(password) + lowCaseTest(password) + upCaseTest(password);
+ digitTest(password);
}
// if conditions for password are met, display message.
cout << "\nYou entered a valid password.\n\n";
return 0;
}
//*********LENGTH TEST FUNCTION***********
bool lengthTest(char *str)
{
int numChar = 0;
bool validlength = false;
for (int cnt = 0; cnt < SIZE; cnt++)
{
while (*str != 0)
str++, numChar++;
}
if (numChar >= 6)
validlength = true;
return validlength;
}
//*********LOWERCASE LETTER TEST FUNCTION*********
bool lowCaseTest(char pass[])
{
for (int cnt = 0; cnt < SIZE; cnt++)
{
if (islower(pass[cnt]))
return true;
}
return false;
}
//********CAPITAL LETTER TEST FUNCTION*********
bool upCaseTest(char pass[])
{
for (int cnt = 0; cnt < 20; cnt++)
{
if (isupper(pass[cnt]))
return true;
}
return false;
}
//**********DIGIT TEST FUNCTION************
bool digitTest(char pass[])
{
for (int cnt = 0; cnt < 20; cnt++)
{
if (isdigit(pass[cnt]))
return true;
}
return false;
}