I am a beginner learning how to code on C++
Any help is very appreciated...
It runs, but not correctly.....
program needs to validate a password:
must be at least 8 characters
must have 2 digits in it
must not have any characters other than letters and numbers
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
|
#include<iostream>
#include <string>
using namespace std;
int main()
{
string Password;
bool correctPassword = false;
char ch = 0; //<-- you need to unitialize this varaible
int number = 0;
while (correctPassword == false)
{
//Accept a conditionally valid password from the user
cout << "Enter a password: ";
cin >> Password;
correctPassword = true;
//Make sure the length is at least 8
if (Password.length() < 8)
{
correctPassword = false;
cout << "Your password must be at least 8 characters\n";
continue;
}
//Verifying password contains at least 2 digits
//correctPassword = false;
for (int i = 0; i < Password.length(); i++)
{
//If password contains anything other than letters or numbers it returns invalid input
if ((isdigit(ch)) || (isalpha(ch)))
correctPassword = true;
cout << "You have entered an invalid character.\n";
// if password doesn't have at least two digits
if (isdigit)
number++; //<-- added ;
}
if (number < 2)
cout << " You must have at least two digits in the password." << endl;
//If false, it doesn't contain any digits
//if (correctPassword == false)
//{
//cout << "Your password must contain at least 2 digits. \n ";
//}
}
//Results password as correct
cout << "Your password is: " << Password << endl;
//end program
return 0;
}
|
It looks like this when it compiles:
Enter your password:
snapper
Your password must be at least 8 characters
Enter a password:
snapper1
You have entered an invalid character
You have entered an invalid character
You have entered an invalid character
You have entered an invalid character
You have entered an invalid character
You have entered an invalid character
You have entered an invalid character
You have entered an invalid character
Your password is: snapper1
So it isn't recognizing that there is only 1 digit, instead of two
and it thinks there is an invalid character...but there is not
Now what?