Help polishing this pin/password generator.
Apr 26, 2012 at 6:27pm UTC
Hello, long time troller first time poster here. I know it's probably been done to death but, I wrote this password and pin generator for the fun of it. I know my logic probably isn't the most rocking and it could be simplified greatly. I was just wondering if anybody had any suggestions to help me with input validation. For example, if someone enters another character besides an int for the "enter desired pin/password length." Also how could I quit from the same spot by using a character instead of the int -1?
Here is the source code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
#include "getRandomCharacter.h"
using namespace std;
void main()
{
int a = 0;
int b = 0;
srand(time(0));
string word;
string pin;
string select = "" ;
ofstream output;
while (select != "Q" )
{
cout << "Enter \"pin\" for PIN gen. Enter \"pass\" for password gen. Enter Q to quit: " ;
cin >> select;
cout << "\n\n\n" ;
if (select == "pin" )
{
while (b != -1)
{
cout << "Enter length of desired PIN number, -1 to quit: " ;
cin >> b;
if (b < -1)
{
cout << "\n\n\n" ;
cout << "Invalid input try again" << endl;
}
cout << "\n\n\n" ;
for (int i = 0; i < b; i++)
{
char ch = getRandomNumber();
pin = pin + ch;
}
if (b > -1)
{
output.open("pins.txt" , ios::out | ios::app);
cout << "Your new pin is: " << pin;
output << pin << endl;
output << "\n\n" ;
output.close();
pin = "" ;
select = "" ;
cout << "\n\n\n" ;
}
}
}
if (select == "pass" )
{
while (a != -1)
{
cout << "Enter length of desired Password, -1 to quit: " ;
cin >> a;
if (a < -1)
{
cout << "\n\n\n" ;
cout << "Invalid input try again" << endl;
}
cout << "\n\n\n" ;
for (int i = 0; i < a; i++)
{
char ch = getRandomCharacter();
word = word + ch;
}
if (a > -1)
{
output.open("passwords.txt" , ios::out | ios::app);
cout << "Your new password is: " << word << endl;
output << word << endl;
cout << "\n\n" ;
output.close();
word = "" ;
select = "" ;
cout << "\n\n\n" ;
}
}
}
}
system("pause" );
}
Here is the getRandomCharacter header file code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <cstdlib>
using namespace std;
char getRandomCharacter(char ch1, char ch2)
{
return static_cast <char >(ch1 + rand() % (ch2 - ch1 + 1));
}
char getRandomCharacter()
{
return getRandomCharacter(33, 126);
}
char getRandomNumber()
{
return getRandomCharacter(48, 57);
}
Topic archived. No new replies allowed.