Created
January 26, 2017 03:28
-
-
Save RyanGFerguson/48b90812684eeb5638be0c90f50c080e to your computer and use it in GitHub Desktop.
a simple password generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <string> | |
#include <cstdlib> | |
#include <ctime> | |
#include <iostream> | |
using namespace std; | |
string passwordGenerator(); | |
int main() | |
{ | |
cout << passwordGenerator() << endl; // used for testing purposes to display the array. | |
return 0; | |
} | |
string passwordGenerator() { | |
char upperCase[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" }; // Declaring character arrays to pull random characters from. | |
char lowerCase[] = "abcdefghijklmnopqrstuvwxyz"; | |
char numbers[] = "0123456789"; | |
const int numOfCharacters = 8; // the length of the password. | |
char password[numOfCharacters +1]; // where the random characters will be stored. | |
srand(time(0)); // Srand(time(0)) is used to reset the root used in the rand function | |
for (int x = 0; x < numOfCharacters; x++) { | |
int arrChoice = rand() % 3; // used to randomly choose what type of character goes next. | |
int randIndex; | |
switch (arrChoice) { // Switch statement used to decide which array to choose from and which index to assign to the pass word generator; | |
case 0: | |
randIndex = rand() % 9; | |
password[x] = numbers[randIndex]; | |
break; | |
case 1: | |
randIndex = rand() % 25; | |
password[x] = lowerCase[randIndex]; | |
break; | |
case 2: | |
randIndex = rand() % 25; | |
password[x] = upperCase[randIndex]; | |
break; | |
default: | |
password[x] = randIndex = rand() % 25; | |
password[x] = upperCase[randIndex]; | |
break; | |
} | |
} | |
password[numOfCharacters] = '\0'; // adds a NULL character to the end of the array. | |
string newPassword(password); // converts the array into a string. | |
return newPassword; // returns the String to be printed to the console | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment