密码程序

Password program

本文关键字:程序 密码      更新时间:2023-10-16
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
int main()
{
    fstream fin;
    string password;
    cout << "Please enter your password!" << endl;
    cin >> password;
    fin.open("Text.txt");
    int nlen = password.length();
    if (nlen <= 7)
        return false;
    if (nlen >= 8)
        return true;
    bool hasUpp = false;
    bool hasLow = false;
    bool hasDig = false;
    bool hasSym = false;
    for (int i = 0; i < nlen; i++)
    {
        if (isupper(password[i]))
            hasUpp = true;
        if (islower(password[i]))
            hasLow = true;
        if (isdigit(password[i]))
            hasDig = true;
    }
    if (hasLow && hasUpp && hasDig && hasSym)
    {
        return true;
    }
    else
    {
        return false;
    }
    if (hasLow && hasUpp && hasDig && hasSym)
    {
        cout << "Your password is strong! " << endl;
    }
    else
    {
        cout << "Your password is too weak! " << endl;
    }
    cin.get();
    cin.get();
    return 0;
}

该程序应该从用户那里获取输入数据,并确定它是否是一个有点强的密码。我意识到这还没有接近完成。我遇到的问题是让程序读取我的输入文件并弄清楚输入文件中的任何单词是否作为密码输入,然后告诉他们他们的密码是错误的。

我对你的程序进行了一些修改,使其至少可以处理用户输入数据。您可能想知道为什么没有从程序中获得任何输出?程序中有两个 if 子句会导致 main(( 函数返回(= 应用程序终止(:

if (nlen <= 7)
    return false;
if (nlen >= 8)
    return true;

调用其中一个 return 语句是因为其中一个 if 子句始终为真,因此您的程序将退出该语句。

以下是处理用户输入的单个密码的修改代码:

#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
int main()
{
    string password;
    cout << "Please enter your password!" << endl;
    cin >> password;
    int nlen = password.length();
    bool hasUpp = false;
    bool hasLow = false;
    bool hasDig = false;
    bool hasSym = false;
    bool isLong = false;
    if (nlen >= 8)
        isLong = false;
    for (int i = 0; i < nlen; i++)
    {
        if (isupper(password[i]))
            hasUpp = true;
        if (islower(password[i]))
            hasLow = true;
        if (isdigit(password[i]))
            hasDig = true;
    }
    if (hasLow && hasUpp && hasDig && hasSym && isLong)
    {
        cout << "Your password is strong! " << endl;
    }
    else
    {
        cout << "Your password is too weak! " << endl;
    }
    cin.get();
    cin.get();
    return 0;
}

要将其扩展为从文件中读取多个密码,您必须逐行读取文件(如此处所述(并处理每一行。

**YOU CAN DO LIKE THIS**
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
void gotox(int x)
{
COORD xy = {0, 0};
xy.X = x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xy);
}
void getx(int &x) {
CONSOLE_SCREEN_BUFFER_INFO csbi;
if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
x = csbi.dwCursorPosition.X;
}
}
int main()
{
SetConsoleTitle("file password protector");
char pas;
string password = "";
int x,t = 0;
cout<<"enter password : ";
do
{
pas = _getch();
switch(int(pas))
{
case 8: getx(x);
        if(x>17)
        {
        --x;
        gotox(x);
        cout<<" ";
        if(password.length()>0){password.erase(password.length()-1,1);}
        --t;
        gotox(x);
        }
        break;
case 27: return 0;
case 13: if(t>8)
        {
           pass = 27
        }
        break;
default :if(t < 30)
        {
            if(int(pas)>0)
                {
                    password.push_back(pas);cout<<"*";++t;
                }
                else
                {
                        pas = _getch();
                }
        }}}while(pas != 13);
bool hasUpp = false;
bool hasLow = false;
bool hasDig = false;
bool hasSym = false;
for (int i = 0; i < t; i++)
{
if (isupper(password[i]))
    hasUpp = true;
if (islower(password[i]))
    hasLow = true;
if (isdigit(password[i]))
    hasDig = true;
}
if (hasLow && hasUpp && hasDig && hasSym)
{
cout << "Your password is strong! " << endl;
}
else
{
cout << "Your password is too weak! " << endl;
}
_getch();
return 0;
}