Hi everyone,
I am currently working on a homework assignment for my starting out in C++ class and am having a hard time either with the format of the loops I am using or perhaps something else I am just not seeing. I have read all through my text book and have Googled everything I could think of to try and get it to work properly. If anyone has any input on what I am doing wrong it would be greatly appreciated.
My assignment is:
Write a program that requests a password and verifies that it is a valid
password. To be valid the password must be at least 6 characters long and
contain at least one digit.
Your program should:
1. Determine whether the password is valid. If it is not, a message telling
the user what is wrong should be output
2. If it is valid, the program should thank the user for entering a valid
password
Sample Output (inputs in bold)
Please enter a password: pass6
Passwords must be at least 6 characters long
Please enter a password: TarrantNW
Passwords must include at least one digit (1-9)
Please enter a password: Tccd03
Thank you, that is a valid password
And this is what I have written
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
|
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
//Function prototypes
bool testDigits(char []);
bool testLength (int);
const int size = 90;
char password[size];
int length;
int main()
{
cout << "Please enter a password: ";
cin >> password;
length = strlen(password);
testLength(length); //To test password length
testDigits(password); //To test password for numeric digits
if (testLength(length) && testDigits(password))//If both functions return true
cout << "Thank you, that is a valid password.\n";
else
cout << "That is an invalid password.\n";
system("pause");
return 0;
}
bool testDigits(char pass[])
{
int len = strlen(pass);
for (int i = 0; i < len; ++i)
{
while (!isdigit(pass[i]))
{
cout << "Passwords must contain one digit (1-9)\n";
cout << "Please enter a password: ";
cin >> pass;
}
if (isdigit(pass[i]))
return true;
}
}
bool testLength(int len)
{
if (len < 6);
{
cout << "Passwords must be at least 6 characters long\n";
cout << "Please enter a password: ";
cin >> password;
len = strlen(password);
}
if (len >= 6)
return true;
}
|