Creating a Password

I am required to create a program that asks a user for a password. The password must contain no less than 6 chars and have one upper case letter, one lower case letter, and have a digit. This is what I have so far but not sure how to put the limits on it. Any help would be greatly appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
	const char SecretWord[70] = "PLP";
	string Password;

	cout << "Please enter a password.  The password must contain the following:\n";
	cout << "Atleast 6 characters\n";
	cout << "Atleast one uppercase and one lowercase letter\n";
	cout << "Atleast one digit\n";
	cin >> Password;

	if ( Password == SecretWord)
		cout << "Password Accepted \n";
	else
		cout << "Invalid Password \n";
	
	return 0;
}

Last edited on
I would suggest a do/while loop ( so it continues when there is no lowercase/uppercase or is is less than 6 or has no integer digits )

Then you can either loop through them to check for upper/lower like this

1
2
3
4
5
6
for( int i = 0; i < SIZE_OF_STRING; ++i )
{
    if( password[i] >= 'a' && password[i] <= 'Z' ) ++lowercase;
    else if( password[i] >= 'A' && password[i] <= 'Z' ) ++ uppercase;
    else if( password[i] >= '0' && password[i] <= '9' ) ++integer; //I wouldnt use an else because what if they input '#' or '&'
}


or you can use the islower/isupper/isdigit

on a side note you may want to use ifstream and ofstream to store the password
Last edited on
by "limits" do you mean like "You have X tries to enter password" ??. if so, try a do-while and a counter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//psuedo--


     // enter password
     
      if(Password== SecretWord)
      { 
       //password accepted
     
       }
do{
    else
    {
     //invalid
     ++counter;
    }

}while (counter<X );


something like that?, play around with it see what you discover.

Topic archived. No new replies allowed.