C ++帮助,密码验证程序

时间:2015-05-29 16:51:34

标签: c++ visual-c++

抱歉,如果我听起来像个白痴,或者我的代码本身很糟糕,但我需要得到所有帮助。很多人都写了这段代码,但我不想看他们的,基本上是复制和粘贴。所以这就是问题,当我尝试运行这个程序时,它给了我标识符_TCHAR未定义,并在第20行给出了一个警告“< signed / unsigned mismatch”。再次,我会喜欢我能得到的任何帮助。

#include <iostream>
#include <cstring>

using namespace std;


int main(int argc, _TCHAR* argv[])

{
    const int size = 1000;

    char password[size];

    int count;

    int times1 = 0;

    int times2 = 0;

    int times3 = 0;
    cout << "Please enter your password: ";
    cin.getline(password, size);


    if (strlen(password) < 6){

        cout << "Not valid, your password should be atleast 6 letters";

    }

    for (count = 0; count < strlen(password); count++)
    {

        if (isupper(password[count])) {

            times1++;

        }

        if (islower(password[count])){

            times2++;

        }

        if (isdigit(password[count])){

            times3++;

        }

    }

    if (times1 == 0) {

        cout << "Invalid, the password should contain atleast one uppercase letter";

    }

    if (times2 == 0) {

        cout << "Invalid, the password should contain atleast one lowercase letter";

    }

    if (times3 == 0) {

        cout << "Invalid, the password should contain atleast one digit";

    }



    cin.get();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

将所有内容包装在while循环中(从times1 = 0,times2 = 0,times3 = 0到cin.get()之前)。使用名为validPass的bool变量并初始化为true。当其中一个要求失败时,只需make validPass = false。 while应为while(validPass == false){...}

#include "stdafx.h"
#include <iostream>
#include <cstring>

using namespace std;


int main()

{
    const int size = 1000;

    char password[size];

    int count;

    bool validPass;
    do
    {
        validPass = true;
        int times1 = 0;

        int times2 = 0;

        int times3 = 0;
        cout << "Please enter your password: ";
        cin.getline(password, size);


        if (strlen(password) < 6){

            cout << "Not valid, your password should be atleast 6 letters";
            validPass = false;
            continue;

        }

        for (count = 0; count < strlen(password); count++)
        {

            if (isupper(password[count])) {

                times1++;

            }

            if (islower(password[count])){

                times2++;

            }

            if (isdigit(password[count])){

                times3++;

            }

        }

        if (times1 == 0) {

            cout << "Invalid, the password should contain atleast one uppercase letter";
            validPass = false;
            continue;

        }

        if (times2 == 0) {

            cout << "Invalid, the password should contain atleast one lowercase letter";
            validPass = false;
            continue;

        }

        if (times3 == 0) {

            cout << "Invalid, the password should contain atleast one digit";
            validPass = false;
            continue;

        }

    } while (!validPass);

        cin.get();
    return 0;
}