My program is supposed take a user input to create a password. There's specific criteria.
The user should be able to retry up five times to create a "good" password. If they make a "good" password, the program should just end. If they don't make a password within the 5 tries, then the program should end. I tried using do-while and a for loop, but it won't break or end the program.
I have a text file with 5 passwords. The user can't use those 5 passwords. I put the passwords into an char array. I'm not sure how to compare the passwords in the file to the user input.
I can only use cstrings for this program. I'm not allowed to use any strings.
"prevPswds.txt"
2347UCDa!
PassUCD97~
@489sqUCD
123AaaUCD$%
UCDAsue1,
main.cpp:
#include <iostream>
#include <cstring>
#include <cctype>
#include <fstream>
//#include <string.h>
using namespace std;
int main()
{
//Initialize variables
const int size = 15;
char* check = NULL;
char checkUCD[] = "UCD";
char specialChar[] = { '~', '!', '@', '#', '$', '%', '^', '&', '*', '-', '_', '?' };
char password[size];
int counter=0;
bool length = false;
bool uppercase = false;
bool lowercase = false;
bool number = false;
bool special = false;
bool ucd = false;
bool previous = false;
bool lower = false;
bool upper = false;
bool num = false;
//bool done = false;
ifstream file("prevPswds.txt");
const int arraySize = 150;
char myArray[arraySize];
char current_char;
int count = 0;
//Read in file
for (int k = 0; k < arraySize; k++)
{
if (file.is_open())
{
int c = 0;
while (!file.eof())
{
file.get(myArray[c]);
c++;
count++;
}
}
}
for (int i = 0; i < 5; i++)
{
cout << "----CREATE A PASSWORD----" << endl;
cout << "Password requires:" << endl;
cout << "-8 to 12 characters" << endl;
cout << "-At least 1 uppercase letter" << endl;
cout << "-At least 1 lowercase letter" << endl;
cout << "-At least 1 number" << endl;
cout << "-At least 1 special character (~, !, @, #, $, %, ^, &, *, -, _, ?)" << endl;
cout << "-'UCD' \n" << endl;
cout << "Password cannot include:" << endl;
cout << "-Lowercase letters 'l,' 'i,' 'o,' or 'z'" << endl;
cout << "-Uppercase letters 'I,' 'O,' or 'S'" << endl;
cout << "-Numbers '0,' '1,' or '5'" << endl;
cout << "-------------------------" << endl;
//Get user input
cout << "Please enter a password." << endl;
cin.getline(password, size);
cout << endl;
counter++;
//Check requirements
if (strlen(password) < 8 || strlen(password) > 12)
{
cout << "-Password must be 8 - 12 characters." << endl;
}
else
{
length = true;
}
for (int i = 0; i < size; i++)
{
if (isupper(password[i])) //Check for uppercase
{
uppercase = true;
}
if (islower(password[i])) //Check for lowercase
{
lowercase = true;
}
if (isdigit(password[i])) //Check for numbers
{
number = true;
}
if (password[i] != 'l' || password[i] != 'i' || password[i] != 'o' || password[i] != 'z') //Check for exceptions
{
lower = true;
}
if (password[i] != 'I' || password[i] != 'O' || password[i] != 'S') //Exceptions
{
upper = true;
}
if (password[i] != '0' || password[i] != '1' || password[i] != '5') //Exceptions
{
num = true;
}
}
for (int i = 0; i < size; i++) //Check for special characters
{
if (specialChar[i])
{
if (ispunct(password[i]))
{
special = true;
}
}
}
check = strstr(password, checkUCD); //Check for 'UCD'
if (check)
{
ucd = true;
}
//Give feedback and suggestion
if (uppercase == false)
{
cout << "Password must contain at least 1 uppercase letter." << endl;
}
if (lowercase == false)
{
cout << "Password must contain at least 1 lowercase letter." << endl;
}
if (number == false)
{
cout << "Password must contain at least 1 number." << endl;
}
if (special == false)
{
cout << "Password must contain at least 1 special character." << endl;
}
if (ucd == false)
{
cout << "Password must contain 'UCD.'" << endl;
}
if (lower == false)
{
cout << "Password cannot contain 'l', 'i', 'o', or 'z.'" << endl;
}
if (upper == false)
{
cout << "Password cannot contain 'I', 'O', or 'S.'" << endl;
}
if (num == false)
{
cout << "Password cannot contain '0', '1' or '5.'" << endl;
}
}
if (length == true || uppercase == true || lowercase == true || number == true || special == true || ucd == true || previous == true || lower == true || upper == true || num == true)
{
return 1;
}
}