簡體   English   中英

如何讓我的 checkLength 函數循環直到用戶輸入超過 5 個字符的密碼?

[英]How do I get my checkLength function to loop until the user enters in a password of more than 5 characters?

好的,所以我正在工作一個項目,我們在其中存儲用戶輸入以創建用戶名和密碼並將其存儲在向量中。 我的主要問題是,每次我使用 checkLength 函數時,它仍然需要用戶的密碼,即使它少於 5 個字符,例如這是我得到的輸出'

Enter your first name.
John
Enter your last name.
Stewart
Enter a password.
abc
Password is not 5 characters.  please try again...

/**it displays the message but still 
   continues instead of making the user input a 5 character password**/

Enter your first name.
Example
Enter your last name.
exmaple
Enter a password.
prob
Password is not 5 characters.  please try again...
Enter your first name.
0
  Login data entered

  JStewa, abc
  Eexmap, prob
Press any key to continue . . .

即使用戶只輸入了三個字符的密碼,我該如何解決這個問題或為什么它會循環!

這是我的代碼

//DISPLAY 8.9 Using a Vector
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#include "c:\Users\barta\OneDrive\Documents\Visual Studio 2015\Projects\Project 7\Project 7\Validate.h"

int main( )
{

    vector<string> data;
    string firstName, lastName, login, userName, password;
    Validate c;

    firstName = "1";

    cout << "Enter your first name.\n";
    getline(cin, firstName);
    while (firstName != "0")
    {

        cout << "Enter your last name.\n";
        getline(cin, lastName);
        do
        {
            cout << "Enter a password.\n";
            getline(cin, password);

            c.checkLength(password);

            lastName = lastName.substr(0, 5);

            userName = firstName.at(0) + lastName;
            login = userName + ", " + password;
            data.push_back(login);
        } while (false);

        cout << "Enter your first name.\n";
        getline(cin, firstName);
    }

    cout << "  Login data entered \n" << endl;

        for (unsigned int i = 0; i < data.size(); i++)
        {
            cout << "  " << data.at(i) << endl;
        }



    system("pause");
    return 0;
}

驗證頭文件:

class Validate
{
public: 
    Validate(string);
    Validate();
    bool checkLength(string);
    bool checkSpaces();
    bool checkUpper();
private:
    string password;
    static const int LEN = 5;

};
Validate::Validate()
{

}
Validate::Validate(string pass)
{
    pass = password;
}
bool Validate:: checkLength(string password)
{

        if (password.length() < LEN)
        {
            cout << "Password is not 5 characters." << "  please try again..." << endl;
            return true;
        }
        else
        {
            return false;
        }

}

循環檢查長度並在循環外執行其他所有操作

bool badLength;
do
{
    cout << "Enter a password.\n";
    getline(cin, password);
    badLength = checkLength(password);

} while (badLength);

lastName = lastName.substr(0, 5);
userName = firstName.at(0) + lastName;
login = userName + ", " + password;
data.push_back(login);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM