我如何在main中检查布尔变量,以便我的do-while循环遍历三个函数?

时间:2016-05-08 02:51:27

标签: c++ string vector project

好吧,我的问题是我似乎无法找到或想到任何会进入while参数的东西,它会检查所有三个函数,看看每个函数是否都是假的,请帮忙!那是我的问题!

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

};
Validate::Validate()
{

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

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

}
bool Validate::checkSpaces()
{
    if (password.find(' ') != string::npos)
    {
        cout << "Password can not have any spaces " << endl;
        return true;
    }
    else
        return false;
}
bool Validate::checkUpper()
{

    for (int i = 0; i < password.length(); i++)
    {
        if (!isupper(password[i]))
        {
            cout << "Password must have at least one uppercase" << endl;
            return true;
        }
        else
            return false;
    }



}

主档

//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;
    bool check;
    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);
            //password = c.checkLength();
            //password = c.checkSpaces();
            c.checkUpper();

        } while (false);

        lastName = lastName.substr(0, 5);

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

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

1 个答案:

答案 0 :(得分:0)

您的问题有点不清楚,但我认为您正在寻找:

 bool flag;

 do
 {
        // All your existing code is here...

        Validate c{password};

        flag = c.checkLength() || c.checkSpaces() || c.checkUpper();

 } while (flag);

您的Validate类的构造函数将密码作为参数,因此您必须在读取密码后构建对象,而不是在main()的开头。