Thread: I need help understanding this code

  1. #1
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    Exclamation I need help understanding this code

    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    
    using namespace std;
    
    
    int main() {
    
    	srand ( time ( NULL ) );
    
    
    	unsigned short int counterN; // counter for Number of passWords 
    	unsigned short int NumberOfPassWord; // Number of passwords
    	unsigned short int PassWordLong; // How Long The PassWord 
    	char Alphabet [74] = {'a','b','c','d','e','f','g','h','i','j','k','l',
    
    		'm','n','o','p','q','r','s','t','u','v','w','x','y','z','A',
    		
    		'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
    		
    		'Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4',
    		
    		'5','6','7','8','9','!','@','#','$','%','^','&','*','(',')',
    		
    		'?','~' };
    
    	cout << "Enter length of password\n>";
    	cin >> PassWordLong;
    
    	cout << "Enter number of passwords\n>";
    	cin >> NumberOfPassWord;
    
    	for ( counterN = 1; counterN <= NumberOfPassWord; counterN++ ) {
    	  
    
    			for ( ; PassWordLong > 0; PassWordLong-- )
    
    
    				cout << Alphabet [ rand() % 74 ];
    		
    
    			cout << endl;
    
    		
    	}
    
    
    	system("pause");
    
    	return(0);
    
    
    }
    Why doesn't this work!!!!!!

    But this dose....
    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    
    using namespace std;
    
    void Long (int passwordlong);
    
    
    int main() {
    
    	srand ( time ( NULL ) );
    
    
    	unsigned short int counterN; // counter for Number of passWords 
    	unsigned short int NumberOfPassWord; // Number of passwords
    	unsigned short int PassWordLong; // How Long The PassWord 
    
    
    	cout << "Enter length of password\n>";
    	cin >> PassWordLong;
    
    	cout << "Enter number of passwords\n>";
    	cin >> NumberOfPassWord;
    
    	for ( counterN = 1; counterN <= NumberOfPassWord; counterN++ ) {
    	  
    			Long( PassWordLong);
    
    			cout << endl;
    
    	}
    
    
    	system("pause");
    
    	return(0);
    
    
    }
    
    void Long ( int PassWordLong ) {
    
    		char Alphabet [74] = {'a','b','c','d','e','f','g','h','i','j','k','l',
    
    		'm','n','o','p','q','r','s','t','u','v','w','x','y','z','A',
    		
    		'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
    		
    		'Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4',
    		
    		'5','6','7','8','9','!','@','#','$','%','^','&','*','(',')',
    		
    		'?','~' };
    
    
    			for ( ; PassWordLong > 0; PassWordLong-- )
    
    
    				cout << Alphabet [ rand() % 74 ];
    
    }
    Can someone help me out..... I got it to work but that I don't understand why the first one doesn't work its mind boggling......
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What doesn't work about it? If you mean, why can I only print one password, that's because you don't reset PassWordLong after printing the first one out.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In the first version, the PassWordLong is 0 at the end of the first iteration of the outer loop. Consequently, the inner loop never runs on subsequent iterations of the outer loop. You need another counter (and personally I would rather count from 0 to PassWordLong than count in reverse).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57
    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    
    using namespace std;
    
    
    int main() {
    
    	srand ( time ( NULL ) );
    
    
    	unsigned short int counterL; // counter for how long the password;
    	unsigned short int counterN; // counter for Number of passWords 
    	unsigned short int NumberOfPassWord; // Number of passwords
    	unsigned short int PassWordLong; // How Long The PassWord 
    	char Alphabet [74] = {'a','b','c','d','e','f','g','h','i','j','k','l',
    
    		'm','n','o','p','q','r','s','t','u','v','w','x','y','z','A',
    		
    		'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
    		
    		'Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4',
    		
    		'5','6','7','8','9','!','@','#','$','%','^','&','*','(',')',
    		
    		'?','~' };
    
    	cout << "Enter length of password\n>";
    	cin >> PassWordLong;
    
    	cout << "Enter number of passwords\n>";
    	cin >> NumberOfPassWord;
    
    	for ( counterN = 1; counterN <= NumberOfPassWord; counterN++ ) {
    	
    		for ( counterL = 1; counterL < PassWordLong; counterL++ )  
    
    			for ( ; PassWordLong > 0; PassWordLong-- )
    
    
    				cout << Alphabet [ rand() % 74 ];
    
    
    			cout << endl;
    
    		
    
    	}
    
    
    	system("pause");
    
    	return(0);
    
    
    }
    Like this b/c this doesn't work??
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, like this:
    Code:
    for ( counterN = 1; counterN <= NumberOfPassWord; counterN++ ) {
        for ( counterL = 0; counterL < PassWordLong; counterL++ )
            cout << Alphabet [ rand() % 74 ] << endl;
    }
    Also, you should declare variables near first use, so I would write:
    Code:
    for ( unsigned short int counterN = 1; counterN <= NumberOfPassWord; counterN++ ) {
        for ( unsigned short int counterL = 0; counterL < PassWordLong; counterL++ )
            cout << Alphabet [ rand() % 74 ] << endl;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57
    Thanks for the tips and help..... Now do you have any idea how to filter out all the passwords that are printed out with the first character as number or spacial char
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  2. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM