Making password requirements.

So.. my teacher asked me to make a program that asks for a new password and asks the user to confirm the password followed by checking if the password and the confirmed password are the same. He also said that it would be nice if you could make it so that you need to have atleast one uppercase letter, one number, and one special character in the password (stuff like !"# I guess) for it to be accepted. Any ideas on how I could try to do this??






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>
using namespace std;

int main() {
	
	
	string uusiSalasana;
	string uusiVahvistus;
	int valikko;

do {
	
			cout << "1. \tMuuta salasanaa. " << endl;
			cout << "2. \tLopeta. " << endl;
			cin >> valikko;
	
	
}while(valikko <1 || valikko >2);

if (valikko == 1) {
			
		do {
	
			cout << "Anna uusi salasana: " << flush;
			cin >> uusiSalasana;
			cout << "Vahvista salasana: " << flush;
			cin >> uusiVahvistus;
	
	
}		while(uusiSalasana != uusiVahvistus);

cout << "Salasana vaihdettu, uusi salasanasi on " << uusiVahvistus << ",ohjelma sulkeutuu..." << endl;
		
}
	
	

if (valikko == 2) {
	
			cout << "Lopetetaan ohjelma... " << endl;
			cin.get();
			cin.get();
			return 0;
}
	
	
	
	
	
	cin.get();
	cin.get();
	return 0;
}
Last edited on
Hi,

1. It's better to program in English.
2. You could create a character array that consists of a bunch of special characters.

char arr[6] = {'!', '@', '#', '$', '%', '&'};

You basically loop through your password, checking each of these special characters with each character in the password.

3. For the Number part, you can use isdigit -
http://www.cplusplus.com/reference/cctype/isdigit/


4. For the uppercase part, you can use isupper -
http://www.cplusplus.com/reference/cctype/isupper/
Last edited on
Umm.. that's all cool and all but I don't know how to use any of that, I don't know what kind of loop I need to use or how to use it to check the the special characters :o

I searched on google for hours on how other people did these and I don't want to copy them because I don't understand them. Consider me a retard, you have to be very specific :D
Sorry if I'm being a pain in the butt, I just don't know how to do this and I'd like to learn :s

Made the code English so it's easier to understand for others :)




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>
using namespace std;

int main() {
	
	char row[9] = { '!', '#', '¤', '%', '&', '@', '£', '$', '€' };
	string newPassword;
	string newConfirmation;

			
		do {
	
			cout << "Enter a new password: " << flush;
			cin >> newPassword;
			cout << "Confirm the password: " << flush;
			cin >> newConfirmation;
	
	
}		while(newPassword != newConfirmation);

cout << "Password changed, your new password is " << newConfirmation << ", program closing..." << endl;



	cin.get();
	cin.get();
	return 0;
}
When using std::string, one could use find_first_of() to check for special character.
Example:
1
2
3
4
5
    std::string word = "rhythm";
    if (word.find_first_of("AEIOUaeiou") != std::string::npos)
        std::cout << word << " contains a vowel ";    
    else
        std::cout << word << " contains no vowels "; 

http://www.cplusplus.com/reference/string/string/find_first_of/
As for the specific task here, I would suggest using a separate function to validate the password. The reason for that is to keep the main body of the code clean and its structure intelligible.

Thus you might have something like this:

1
2
3
4
5
6
7
8
bool validate(std::string password)
{
    // add whatever code needed here to check the password is acceptable.
    // if there is a problem
    return false;
    // else
    return true;
}


Then in main()

1
2
3
4
    if (validate(newPassword))
        cout << "Password changed"
    else
        cout << "Password must contain etc.... ";


Though you would probably be better putting that function call inside your do-while loop and modify the logic to try again if the password is unacceptable.

Thank you Chervil for posting but I still don't understand what I need to do exactly, would be better if I saw a completed program and I knew exactly how it works so I could say "Ok, that makes sense and I know how it works therefore with this knowledge I can now make my own." I'll try to search online for stuff, maybe I will understand soon.
would be better if I saw a completed program

I understand your predicament. However, it's also difficult to offer help without completing the entire task for you. Finding a balance of providing just the right amount of help is a matter of guesswork as much as judgement.

What both TarikNeaj and myself have offered are what might be considered as building blocks. It's up to you to assemble the blocks to construct - well some sort of program. You don't need to aim for a complete, fully functioning program first time, just try to approach it gradually by adding some of the functionality which you know is needed, and then test the program. There is a cycle of repeated coding, testing, making some changes if required and testing again which is very much a normal part of software development. And posting the latest version of your code on this forum can also be part of that cycle.
Last edited on
Yeah I see what you mean but I don't understand it, by all logic wouldn't I learn to how do it faster if someone did it for me and I analyzed it and made one of my own based on what was given? Why take a loooooooooooong time building something you don't know how to build when you can get a completed one, analyze and UNDERSTAND how to do it, now I can do it too. It's so much faster.

Like asking a child, hey kid, how much is 8 x 8, the kid responds he doesn't know because he's never been taught how to do multiplications. Instead of saying "well you need to do it on your own by figuring it out" you could say " 8 x 8 is 64 because bla bla bla..." and the kid now understands how it works and what is the end value.

And don't be confused, I don't want the complete code because I'm lazy and I just want to copy paste and done! No, I'd like it that way because I will learn A LOT faster as said ^.

Or I'm I just weird?
I don't think anyone is expecting you or the schoolkid to reinvent the wheel, it's simply that one learns by doing. No matter how much time one spends watching someone else swim or ride a bicycle, it isn't until one gets into the water or climbs onto the bike that any real progress is made.

But at the same time, you're not being asked to do this alone. It isn't a matter of jumping in and risking drowning. All the help and support you need is here (not from me - I mean the whole forum community).

Perhaps you have something specific you need to be answered before you can proceed - among all this talk we may have lost sight of the ball. So feel free to get back to the original topic.
Chervil has given you almost everything you need. Here is an example putting it together:
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
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cmath>

using std::string;
using std::cin;
using std::cout;

bool isValid(string password)
{
    // Test 1: does it contain a vowel
    if (password.find_first_of("AEIOUaeiou") == string::npos) {
        return false;
    }

    // Test 2: ...

    // Test 3: ...

    // If you get here then all tests pass
    return true;
}


int
main(int argc, char *argv[])
{
    string password;
    cout << "Enter password: ";
    cin >> password;
    if (!isValid(password)) {
        cout << "Password isn't valid\n";
        return 1;
    }

    cout << "Re-enter password: ";
    string password2;
    cin >> password2;
    if (password != password2) {
        cout << "Passwords don't match\n";
        return 2;
    }

    cout << "Password confirmed\n";
    return 0;
}


This code shows a handy technique when you have to do error checking (in other words, when you're programming in the real world): rather than writing code like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
doSomething();
if (thatsucceeded) {
    doSomething2();
    if (that succeeded) {
       doSomething3();
        if (that succeeded) {
            return success;
        } else {
            handle from something3
        }
    } else {
        handle error from something2
    }
} else {
    handle error from something1
}
return failed;

write it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
doSomething();
if (thatfailed) {
    handle error from doSomething
    return failed;
}

doSomething2();
if (that failed) {
    handle error from doSomething2
    return failed;
}

doSomething3();
if (that failed) {
    handle from something3
    return failed;
}
return success;

It's a little more verbose this way but it's easier to write and easier to maintain, especially if there are 20 things to do instead of just 3.
Thanks guys, I was able to make what I wanted :) May I ask why would you use

std::string;
using std::cin;
using std::cout;

Instead of using "using namespace std;" ?
You could ask this guy - http://bfy.tw/4NwE
Hahaha, that made me laugh, thanks ;)
Topic archived. No new replies allowed.