Okay, now THIS version works right, every time, under every condition.
Quote:
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;
//Function checks the password for length. (a)
bool passLength(char[]);
//Function checks the password for a digit. (b)
bool containDigit(char[]);
int main()
{
const int SIZE = 21;
char password[SIZE];
cout << "Please enter a password: ";
cin.getline(password, SIZE);
while ((!passLength(password)) || (!containDigit(password)))
{
if (!passLength(password))
{
cout << "Passwords must be at least 6 characters long" << endl;
cout << "Please enter a password1: ";
cin.getline(password, SIZE);
(passLength(password)); //(a)
}
if (!containDigit(password))
{
cout << "Passwords must include at least on digit (1-9)" << endl;
cout << "Please enter a password2: ";
cin.getline(password, SIZE);
(containDigit(password)); //(b)
}
}
cout << "Thank you that is a valid password" << endl;
//Keep the window open until Enter key is pressed.
cout << "\nPress Enter to close window..." << endl;
std::cin.get();
return 0;
}
bool passLength(char password[]) //(a)
{
int lengthPass = 6;
int length = strlen(password);
if (lengthPass <= length)
return true;
else return false;
}
bool containDigit(char password[]) //(b)
{
int index = 0;
int length = strlen(password);
for (index = 0; index < length; index++ )
{
if (isdigit(password[index]))
return true;
}
return false;
}
|
Now, I agree it could be more concise. Since this is the first lesson I had to actually stop and think about, I feel like I want to keep working on it. Keep in mind, I already got a 100 on this. Now, it's just for the principle.