This program is supposed to check that a password is at least 6 characters AND contains at least one digit. What I never was able to figure out is how to check for BOTH conditions. Since I have them in two functions, once one is satisfied, it drops to the next one and forgets about the other. I didn't know how to get "back" to the previous condition, so I copy/pasted the code in an if/else statement! The if statement was originally supposed to print the "Thank you that is a valid password" but it didn't print, so I put it in again at the bottom.
However, if you put in a correct password at the first prompt, the if statement prints. In the version I am turning in, I told it to print an endl; (it just skips a line).
See how the "Please enter a password: ";s are numbered 1 through 4? if you get down to "Please enter a password4: "; and enter 123 it accepts this as valid. It still does not check "also" for length.
I don't know how to validate a string of input for two separate conditions. The problem is, when it fails I need it to tell the user why it failed, so I can't just lump them together.
I am going cross-eyed. Can anybody tell me what my ignorant mistake is?
There are no examples of this in my textbook...
Quote:
//This program prompts the user to enter a password,
//and then checks the password against (a) and (b)
//conditions to determine if it is valid. Whenever
//the password fails a checkpoint, the reason is given
//and the user is prompted to enter another password.
//When a correct password is given, the user is informed
//and the program exits anfter the enter key is pressed.
#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)) //(a) called
{
cout << "Passwords must be at least 6 characters long" << endl;
cout << "Please enter a password1: ";
cin.getline(password, SIZE);
(passLength(password)); //(a) called again
}
while (!containDigit(password)) //(b) called
{
cout << "Passwords must include at least on digit (1-9)" << endl;
cout << "Please enter a password2: ";
cin.getline(password, SIZE);
(containDigit(password)); //(b) called again
}
if ((passLength(password)) && (containDigit(password)))
{
cout << "Clearly, I have no idea what I'm doing here..." << endl;
cout << "I copy/pasted the previous code, to check it again. lol." << endl;
cout << "I never really figured out how to check for BOTH conditions."<< endl;
}
else
{
while (!passLength(password)) //(a) called
{
cout << "Passwords must be at least 6 characters long" << endl;
cout << "Please enter a password3: ";
cin.getline(password, SIZE);
(passLength(password)); //(a) called again
}
while (!containDigit(password)) //(b) called
{
cout << "Passwords must include at least on digit (1-9)" << endl;
cout << "Please enter a password4: ";
cin.getline(password, SIZE);
(containDigit(password)); //(b) called again
}
}
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) function
{
int lengthPass = 6;
int length = strlen(password);
if (lengthPass <= length)
return true;
else return false;
}
bool containDigit(char password[]) //(b) function
{
int index = 0;
int length = strlen(password);
for (index = 0; index < length; index++ )
{
if (isdigit(password[index]))
return true;
}
return false;
}
|
I am a total n00b at programming, this is programming fundamentals 1. Please take pity on me. This is due on Nov. 23rd.