[C/C++] Simple Password Generator

Posts 1–15 of 16 · Page 1 of 2
[C/C++] Simple Password Generator
This is a very simple and random password generator code I wrote a long time ago.

Code:
#include <iostream>
#include <cstdlib>
using namespace std;

int main(){
    int num;
    int i=1;
    srand(time(NULL));
    char char_arr[64]={"zAyBxCwDvEuF***HrIqKpLoMnNmOlPkQjRiShTgUfVeWdXcYbZa1928374560!-"};
    while (i==1){
    cout << "How many characters? ";
    cin >> num;
    cout << "\nRandom password: ";
    for (int i=0; i<num; i++)
        cout << char_arr[rand() % 63];
        
         cout << "\n\n";
    cout << "Again? (0 to quit)";
    cin >> i;
    cout << "\n\n";
    }
    
     return 0;
}
As I said, very simple.
thanks now i got somthing els to learn from
i got one too that is much easyer to understand i post that one today
while (i==1){
cout << "How many characters? ";
cin >> num;
cout << "\nRandom password: ";
for (int i=0; i<num; i++)
cout << char_arr[rand() % 63];

cout << "\n\n";
cout << "Again? (0 to quit)";
cin >> i;
cout << "\n\n";
}
change while (i==1) to while (i!=0)

or change

cout << "Again? (0 to quit)"; to cout << "Again? (1 to do it again)";
here is mine:
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>


using namespace std;

int generate(int len);
void main(void)
{
	int exit;
	int length;
	while(true){
		cout << "How many chars?\n";
		cin >> length;
		generate(length);
		cout<<"\n\nExit?\n1=yes\n2=no\n";
		cin>>exit;
		cout<<"\b";
		if (exit==1){
			break;
		}
	}
}

int generate(int len){
	string poss="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	int rand_num;
	srand(time(NULL));
	string pass;
	for (int x=1;x<=len;x++){
		
		rand_num=rand() % 62;
		pass+=poss.at(rand_num);
	}
	cout<<"password: "<<pass<<endl;
	return 0;
}


okay it might not be easyer but i made it this way and im going to make a windows form out of this
Quote Originally Posted by Mr.Magicman View Post

string poss="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGH IJKLMNOPQRSTUVWXYZ";
I have a question, 2 infact.. Im learning C and C++ and is a string supposed to be a character array? Or is there a string datatype?

Should it not be char poss[65]={"bcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKL MNOPQRSTUVWXYZ"};

Please correct me if I am wrong..

And also, is exit not a reserved keyword? So it cant be used as a variable name??
Quote Originally Posted by ijbeed View Post
I have a question, 2 infact.. Im learning C and C++ and is a string supposed to be a character array? Or is there a string datatype?

Should it not be char poss[65]={"bcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKL MNOPQRSTUVWXYZ"};

Please correct me if I am wrong..

And also, is exit not a reserved keyword? So it cant be used as a variable name??
String is sort of like a C++ version of a character array(to make things easier I guess), probably made because C++ is more OO then C and this would be an easy way to demonstrate it

And yep, exit is a reserved keyword
Quote Originally Posted by Hell_Demon View Post
String is sort of like a C++ version of a character array(to make things easier I guess), probably made because C++ is more OO then C and this would be an easy way to demonstrate it

And yep, exit is a reserved keyword
string may be the C++ implementation of a character array, but character arrays still seem to be better. i.e. I couldn't use cin.getline(); with a string, I had to use a character array.(THANKS WHY!)
Nicely done guys . I guess this is just a snippet but otherwise you should write the pass to a file, hard to copy the pass from the console

Oh and just a question iopop9 why do you have cstdlib and string included?
They are not needed for that app, just makes the exe larger
Quote Originally Posted by ctpsolo View Post
Nicely done guys . I guess this is just a snippet but otherwise you should write the pass to a file, hard to copy the pass from the console

Oh and just a question iopop9 why do you have cstdlib and string included?
They are not needed for that app, just makes the exe larger
OFF TOPIC: I'm a noob, I read this, and I figured i'd throw random information in here. I JUST learned this today. xD

Ever see a source code including this line?
[php]#define WIN32_LEAN_AND_MEAN[/php]

I had seen that quite a few times before, and I always wondered what it was. It just looks like such an obscure line of code. But here it is.

It is used to reduce the size of files that use the windows.h header. It excludes some of the less used/more advanced features of the header file. It also speeds up compile time (in large files that make multiple uses of the windows header.) as well as run time, but again, only in larger files. Because compiling 50 lines of code doesn't take too long anyway. =/


While i'm at it, i'll add MSDN's piece on reducing build time with smaller header files.

Quote Originally Posted by MSDN
Faster Builds with Smaller Header Files

You can reduce the size of the Windows header files by excluding some of the less common API declarations as follows:

* Define WIN32_LEAN_AND_MEAN to exclude APIs such as Cryptography, DDE, RPC, Shell, and Windows Sockets.
* Define one or more of the NOapi symbols to exclude the API. For example, NOCOMM excludes the serial communication API. For a list of support NOapi symbols, see Windows.h.
Quote Originally Posted by ilovecookies View Post
OFF TOPIC: I'm a noob, I read this, and I figured i'd throw random information in here. I JUST learned this today. xD

Ever see a source code including this line?
[php]#define WIN32_LEAN_AND_MEAN[/php]

I had seen that quite a few times before, and I always wondered what it was. It just looks like such an obscure line of code. But here it is.

It is used to reduce the size of files that use the windows.h header. It excludes some of the less used/more advanced features of the header file. It also speeds up compile time (in large files that make multiple uses of the windows header.) as well as run time, but again, only in larger files. Because compiling 50 lines of code doesn't take too long anyway. =/


While i'm at it, i'll add MSDN's piece on reducing build time with smaller header files.
I did not know that, thanks for the info!
Should make my apps even more tiny... I learned the hard way that using things as printf/fopen is a lot better than having to deal with string/fstream libs that likes to make the output a lot larger. Funny enough my n00b teacher in C++ at school never talked anything about things like that during a whole 3 years of study, I should probably mentioned that he never got us any longer than making console apps as well...
srry if there's mistakes in the code. I wrote these sources long ago but never compiled it
i was referring to the code written by iopop9
i did my code when i didnt know of arrays BUT it works atleast right?
Quote Originally Posted by Mr.Magicman View Post
i did my code when i didnt know of arrays BUT it works atleast right?
Yep it works like a charm.
Posts 1–15 of 16 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us