密码程序

时间:2014-10-21 17:52:12

标签: c++ if-statement

#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;
}

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

2 个答案:

答案 0 :(得分:0)

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

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

其中一个return语句被调用,因为其中一个if-clause总是为true,因此你的程序将退出那里。

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

#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;
}

要将其扩展为从文件中读取多个密码,您必须逐行读取文件(as explained here)并处理每一行。

答案 1 :(得分: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;
}