Easier solution for nested "for loops"?

So here is simple program that checks every password with 4 characters, it starts with aaaa,aaab,aaac,aaad....aaba,aaca,aada.... and so on. To check for every of those combinations I used loop inside loop inside loop... user->passsword is password of user that we are trying to find with this sea of loops.
CODE LOOKS LIKE THIS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int num_of_digits = user->password.size();
	string my_pass = "aaaa";		
	for (int j = 'a'; j < 'z'; j++) {
		my_pass[0] = j;
		for (int j = 'a'; j < 'z'; j++) {
			my_pass[1] = j;
			for (int j = 'a'; j < 'z'; j++) {
				my_pass[2] = j;
				for (int j = 'a'; j < 'z'; j++) {
					my_pass[3] = j;
					cout << "Try " << my_pass << "=" << user->password << endl;
					if (my_pass == user->password)
					{
						cout << endl << my_pass << "=" << user->password << endl;
						return;
					}
				}
			}
		}
	}
}


So there must be a better way than this especially if we want more than 4 digits.
Thanks in advance guys.
Last edited on
I'm gonna analyse this a little bit but thanks man that should be it.

Here is my answer for future generations:
1
2
3
4
5
6
7
8
9
10
11
void Hack(string &guess_String, string target_String, int string_size) {	
	for (char current_letter = 'a'; current_letter < 'z' + 1; current_letter++) {		//loop for every letter  <-------------------------------------------
		if (guess_String == target_String)                                          //if strings are same exit this and every resursion loop            |
			return;		                                                            //                                                                  |
		if (string_size > 0) {                                                      //check every digit until diget is on 0 place (awfx)->(0123)        |
			guess_String[string_size - 1] = current_letter;                         //                                                                  |
			cout << "Checking..." << guess_String << "=" << target_String << endl;  //                                                                  |
			Hack(guess_String, target_String, string_size - 1);                     //check every digit for one place on left for every letter on right |
		}
	}
}
Last edited on
Topic archived. No new replies allowed.