質問

Hello Everybody i got really an easy issue, but i dont know how to solve it T.T So the question is, why my mainMenu function is not working? and why the loop neither?

I have come with the idea of maybe is the cin.getline?

Update: Sorry everybody i'm new in this webpage and i didn't know the etiquete to ask question, also i'm a beginner with c++.

Ok, what is not working is the loop, for example this is what happen after I compile.

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

After putting g it stop, what actually it needs to do is keep repeating until you make the strong password.

Thank you for your help and your time.

this is my code:

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

}
役に立ちましたか?

解決

To call mainMenu() from main(), change

char passWordInput[1000];
char *password;

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

to

char passWordInput[1000];
char *password;

mainMenu(); // get rid of `void'
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top