Password validation

I am a beginner trying to learn to code for this homework assignment....
I cannot get this program to work no matter what I try and change.
I am so frustrated!!

I do not want to use a switch statement.

The code has to create a password.
Must have at least 8 characters.
Must have at least digits
Can only use letters or numbers in the password

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
#include<iostream>
#include <string>

using namespace std;

int main()
{

	string Password;
	bool correctPassword = false;
	char ch;
	int number;

	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 = 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++
		}
		    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 ";
			continue;
		}
		
			
	    }

	//Results password as correct
	cout << "Your password is: " << Password << endl;

	//end program 
	return 0;
}
Last edited on
Changes are commented or theirs a comment if something needs to be changed

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
#include<iostream>
#include <string>

using namespace std;

int main()
{

	string Password;
	bool correctPassword = false;
	char ch; //<-- you need to unitialize this varaible
	int number = 0;//<-- added = 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 = 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 ";
			continue;
		}


	}

	//Results password as correct
	cout << "Your password is: " << Password << endl;

	//end program 
	return 0;
}
1
2
3
char ch;
// ...
if (!(isdigit(ch)) || !(isalpha(ch)))

ch is uninitialised so it contains a garbage value. When doing operations on them, you will get undefined behaviour. I also don't see ch being assigned to each character of Password.

1
2
3
4
int number;
// ...
if (isdigit)
    number++

Same as ch.
isdigit is a function and expects an argument. When you just write the name of the function, it's a function pointer, which will make this statement always run.
I am sorry to sound dumb,
but how do I fix those?

I changed
int number = 0;

to

int number;

Is this what you meant me to do?

And I do not understand about the ch....
How do I fix that?

Thank you again.
Last edited on
You need to initialise your variables.
1
2
int number = 0; // initialised
char ch = 0; // initialised 


for (int i = 0; i < Password.length(); i = i ++)
You don't need to assign i. i++ does that for you.
for (int i = 0; i < Password.length(); i ++)
So here is what I have done to fix it.
It runs, but not correctly.....
progress...
Any other suggestions??

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?
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/202982/
Topic archived. No new replies allowed.