GeekPolice Tech TutorialsLog in

 


C++ Random Password Generator v1

Share

20100423
C++ Random Password Generator v1

Made a little program that makes a random password using a simple algorithm, going to add number into the equation later but for now, this is simple and will get people going in the right direction if they wanted to make something sort of like this. Feel free to use this code as long as you give credits Big Grin

Code:

#include <iostream>
#include <string>
#include <windows.h>
#include <ctime>
using namespace std;

void clrscr()
{
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coord = {0, 0};
    DWORD count;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(hStdOut, &csbi);
    FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
    SetConsoleCursorPosition(hStdOut, coord);
}

int main()
{
   while (true)
   {
      clrscr();
      srand(static_cast<unsigned int>(time(0)));
      char lower[26] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'y', 'x', 'z' };
      char upper[26] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'Y', 'X', 'Z' };
      string password;
      for (int n = 0; n <= 5; n++)
      {
         password = password + lower[rand() % 25] + upper[rand() % 25];
      }
      cout << "Your generated password:" << endl;
      cout << password << endl;
      cout << "Press 'enter' to generate another." << endl;
      cin.get();
   }
   return 0;
}


Thanks,
- Programmer

Comments

Agent Cosmic
C++ looks more complicated than I expected. :lol2:
GeorgeDorn
Not to be a party pooper but there's not really that much C++ in it. Granted you use std::cout and std::cin instead of the C equivalents printf and scanf and std::string, but it doesn't really add anything that couldn't be done in C with the same amount of code. Ignoring C++'s object oriented features and writing C style procedural code isn't really what C++ is about.

Also, as every character in C and C++ has an ASCII code you might as well just use those instead of declaring entire arrays of characters. A code example of what I mean can be found here:

http://en.wikipedia.org/wiki/Random_password_generator

And since your clrscr() function is WIN32 specific anyway you might as well just use "system("cls");" instead.
avatar
Yeah but redefining the clrscr() function added more challenge and time to the program, which still only took like 10min to make. Yes, you are correct, this could have easily been done with C, but why not make it in C++, it can go either way Big Grin
GeorgeDorn
Heh, yeah. Smile...

But I actually didn't mean C. The ASCII codes are equally valid in C++.
avatar
I was actually referring to the part where you said, "but it doesn't really add anything that couldn't be done in C with the same amount of code." and yeah I know that the ASCII codes are valid in C++ Big Grin
naphets
What i need is a slight variation of the multiple ideas i have read for simple coding of random password generators.
Instead of the output of displaying a single random password... Create a text file, each line is another password... All possibilities in a single file!

Can someone please help me?
dbzsuper
great i thought c++ is seems easy but its quite look hard Goofy other wise Thanks
johnbelley1
i try this code not working properly i thnk there's is a bug in line no 23 can any body guide me ?
Permissions in this forum:
You cannot reply to topics in this forum