C ++为什么我的循环和我的函数不起作用?

时间:2014-04-22 19:01:04

标签: c++ function loops while-loop boolean

大家好我真的很容易问题,但我不知道如何解决它T.T 所以问题是,为什么我的mainMenu函数不起作用?为什么循环呢?

我的想法可能是cin.getline?

更新 对不起每个人我都是这个网页的新手,我不知道提问的问题,我也是c ++的初学者。

好的,循环不起作用,例如这是我编译后发生的事情。

Try a Password: Hello
At least 8 Characters
You need at least one number
You need at least one symbol
Try a Password: g

在停止之后,实际上需要做的是不断重复,直到你输入强密码。

感谢您的帮助和时间。

这是我的代码:

#include <iostream>
#include <ctype.h>
#include <iomanip>
#include <string>
#include <cstdlib>


using namespace std;

// protypes
bool checkPassword(char *);
void mainMenu();


int main(int argc, const char * argv[])
{

char passWordInput[1000];
char *password;

void mainMenu();

cout << "\n" << endl;
cout << " Try a Password: ";
cin.getline(passWordInput, 1000);

password = passWordInput;

while(!checkPassword(passWordInput))
{

    cout << " Try a Password: ";
    cin.getline(passWordInput, 1000);

    password = passWordInput;

}

return 0;
}
void mainMenu()
{

cout << "Your password must be at least 8 characters long and" << endl;
cout << "contain at least one character from the following categories." << endl;
cout << "-------------------------------------------------------------" << endl;
cout << "Uppercase letters: " << "  " << "(ex: A,B,C...)" << endl;
cout << "Lowercase letters: " << "  " << "(ex: a,b,c...)" << endl;
cout << "Numbers: " << setw(38) << "(ex: (0,1,2,3,4,5,6,7,8,9)" << endl;
cout << setw(49) << "----------------------------" << endl;
cout << "Symbols: " << setw(40) << "(ex: ( ! @ # $ ) ( % ^ & * )" << endl;
cout << setw(46) << "(ex: ( _ - + = { } [ ] '\' )" << endl;
cout << setw(49) << "(ex: ( | : ' . < > , . ? / )" << endl;
cout << setw(49) << "----------------------------" << endl;
}

 bool checkPassword(char *password)
{

// What it have?
bool lengthGood = false;
bool hasUpper = false;
bool hasLower = false;
bool hasNumber = false;
bool hasSymbol = false;
bool allGood = true;

// index of every character
int index = 0;
char currentChar;

while ( (currentChar = password[index]) != '\0')
           {


                if (isupper(currentChar))
                {
                    hasUpper = true;
                }
                else if(islower(currentChar))
                {
                    hasLower = true;
                }else if (isdigit(currentChar))
                {
                    hasNumber = true;
                }else if (isgraph(currentChar) && isprint(currentChar) && ispunct(currentChar))
                {
                    hasSymbol = true;
                }
                index += 1;
           }

                if (index >= 8)
                {
                    lengthGood = true;
                }


        if (!lengthGood)
        {
            cout << "At least 8 Characters" << endl;
            allGood = false;
        }
        if (!hasUpper)
        {
            cout << "You need at least one Upper case" << endl;
            allGood = false;
        }
        if (!hasLower)
        {
            cout << "You need at least one lower case" << endl;
            allGood = false;
        }
        if (!hasNumber)
        {
            cout << "You need at least one number" << endl;
            allGood = false;
        }
        if (!hasSymbol)
        {
            cout << "You need at least one symbol" << endl;
            allGood = false;
        }
        if (allGood)
        {
            cout << " Congratulations your password is safe " << endl;
        }

return allGood;

}

1 个答案:

答案 0 :(得分:2)

要从mainMenu()致电main(),请更改

char passWordInput[1000];
char *password;

void mainMenu(); // this is a prototype, not a call

char passWordInput[1000];
char *password;

mainMenu(); // get rid of `void'