Creating a password...help needed

Hello fellas,

Last thing you want to do is probably help me with some HW, but just in case there are some generous souls in an advising mood...

Here's what I got, I have to create a program that asks the user to input a password, and it has to meet 3 criteria;

1. Be at least six char. long.
2. Must contain one capital or one lowercase char.
3. Contain at least one digit.

Here's where I need help...
Do I go about this in a "bool" fashion and check the password after the input and see if each of the criteria are met and set that as "true" and if one isn't set it as "false" and then using "if" functions report back to the user the error?

or

Is it possible to just "if" function the whole part where the user inputs an unusable password...

Also, have no idea how to check for capital letters or digits in the password...I guess thats my number 1 problem.


Here's what I have so far though,

Any help is appreciated, thanks guys!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  //
//  This program will create a password for the user with 3 criteria in mind.
//  HW_9
//
//  Created by Vlad Tumasyan on 4/26/14.
//  Copyright (c) 2014 Vlad Tumasyan. All rights reserved.
//

#include <iostream>
#include <cctype>
#include <cstring>
#include <cstdlib>

using namespace std;

int main()
{
    char pass [6];
    
    //Tells user the criteria for the new password
    cout<<"Please enter a new password \n";
    cout<<"Please remember to include the following \n";
    cout<<"1. Your password must be at least six (6) characters long. \n";
    cout<<"2. Your password must contain at least one uppercase or one lowercase letter. \n";
    cout<<"3. Your password must contain at least one (1) digit. \n \n \n";
    
    //Asks user to input a possible password
    cout<<"Please enter a password with the previous criteria in mind...";
    cin>>pass;
    
    
    cout<<endl <<pass;
    return 0;
}
Do I go about this in a "bool" fashion and check the password after the input and see if each of the criteria are met and set that as "true" and if one isn't set it as "false" and then using "if" functions report back to the user the error?

or

Is it possible to just "if" function the whole part where the user inputs an unusable password...
I don't understand the difference between the two possibilities. Could you give an example of what you'd like to write for each?
Not sure if this is what you're getting at, but typically you'd set it up in a loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(int argc, char* argv[]) {

	std::string string;

	std::cout << "Enter password:\t";
	while (true) {
		std::cin >> string;
		if (isValid(string)) {
			break;
		}
		std::cout << "Try again:\t";
	}

	//do stuff
	return 0;
}
Last edited on
I wish i could give an example, except I don't know how to check for digits or uppercase and lowercase in an array...

what i meant was have a bool set to true for all 3 categories, and if any of them come up as "false" have the user retry the password.

Extending on xismn, you could do it like..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <iostream>
#include <string>
using namespace std;

bool IsValid(string password);

int main()
{
	string pass;

	//Tells user the criteria for the new password
	cout << "Please enter a new password \n";
	cout << "Please remember to include the following \n";
	cout << "1. Your password must be at least six (6) characters long. \n";
	cout << "2. Your password must contain at least one uppercase or one lowercase letter. \n";
	cout << "3. Your password must contain at least one (1) digit. \n \n \n";

	//Asks user to input a possible password
	cout << "Please enter a password with the previous criteria in mind...";
	cin >> pass;

	if (IsValid(pass))
	{
		// im valid, check if its a correct password.
	}

	return 0;
}


bool IsValid(string password)
{
	bool gotAlpha = false, gotDigit = false;
	int len = password.length();
	//wrong length
	if (len < 6)
		return false;

	for (int i = 0; i < len; i++)
	{
		if (password[i] >= 'A' && password[i] <= 'Z' ||
			password[i] >= 'a' && password[i] <= 'z')
			gotAlpha = true;
		else if (password[i] >= '0' && password[i] <= '9')
			gotDigit = true;
	}

	if (gotAlpha && gotDigit)
		return true;			
	else 
		return false;

}
Alternatively, because I advocate the STL and C++11:

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

bool isValid(const std::string& password) {
	//Password must meet three criteria

	//Be at least six characters long
	//Must contain one capital or one lowercase character
	//Must contain one digit

	return (
		(password.size() >= 6) &&
		((std::any_of(password.begin(), password.end(), [](char c){return (c >= 'a' && c <= 'z'); })) ||
		(std::any_of(password.begin(), password.end(), [](char c){return (c >= 'A' && c <= 'Z'); }))) &&
		(std::any_of(password.begin(), password.end(), [](char c){return (c >= '0' && c <= '9'); }))
		);

}

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

	std::string string = "password1";

	std::cout << "\"" << string << "\" is" << (isValid(string)?"":"n't") << " a valid password." << std::endl;

	std::cin.get();
	return 0;
}
Topic archived. No new replies allowed.