Password Check Problem

I need help with this problem I am working on that essentially asks the user to input a password with a few guidelines:
1: at least 8 characters
2: at least 2 digits
3: must consist of only letters and digits

This is what I have so far. The password length works, except it outputs "Password must have at least 8 characters" for ever when you enter less than 8 characters. How do I make it output only once? I am not sure how to do 2 and 3 either

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
#include <iostream>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

  int main()
{
	string inputPassword;
	cout << "Enter a password with the following guidlines:\n\n";
	cout << "\t*Password must have at least 8 characters\n";
	cout << "\t*Password must consist of only letters and numbers\n";
	cout << "\t*Password must be at least 2 characters\n\n";
	getline(cin, inputPassword);
	
	for (int letters = 8; letters > inputPassword.length();)
	{
		cout << "Passsword must have at least 8 characters";
	}
	
	/*int digit = 2;
	while (isdigit(inputPassword[digit]) < 2)
	{
		cout << "Password must have at least 2 characters";
		digit++;
	}*/
	return 0;
}
1
2
3
4
5
for (int letters = 8; letters > inputPassword.length();)
	{
		cout << "Passsword must have at least 8 characters";
	}
	


Should be :
1
2
3
4
if(inputPassword.length() < 8)
	{
		cout << "Passsword must have at least 8 characters" << endl;
	}
Last edited on
 
if(inputPassword.length() <= 8)
@integralfx
if(inputPassword.length() <= 8)
So what is the output you get when you input a password with 8 characters?

P.S : Today is not your day again. I'm sorry.
Last edited on
Okay I fixed the first rule. How would I go about fixing the second rule. I tried this, but theres an error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	string inputPassword;
	cout << "Enter a password with the following guidlines:\n\n";
	cout << "\t*Password must have at least 8 characters\n";
	cout << "\t*Password must consist of only letters and numbers\n";
	cout << "\t*Password must be at least 2 characters\n\n";
	getline(cin, inputPassword);
	
	if(inputPassword.length() <= 8)
	{
		cout << "Passsword must have at least 8 characters";
	}
	
	else if (isdigit(inputPassword.length) < 2)
	{
		cout << "Password must have at least 2 characters";
	}
	return 0;
}
@salomonthesav
Try inputting a password with exactly 8 characters and you will see my point.
@SakurasouBusters Yes you are correct. It can't be less than or equal to because that isolates 8 as well. Do you know how to implement the other 2 rules?
So, at least 2 digits :
1
2
3
4
5
6
7
8
9
10
int numDigits = 0;
for(int i = 0; i < inputPassword.length(); i++)
{
    if(isdigit(inputPassword[i])) numDigits++;
}

if(numDigits < 2)
{
	cout << "Password must have at least 2 digits!" << endl;
}

Last edited on
@SakurasouBusters

I see what you did with the for loop and line 7, but could you explain to me what you did in line 4?
Also how would I go about rule #3?

So far, I got this.

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
int main()
{
	string inputPassword;
	cout << "Enter a password with the following guidlines:\n\n";
	cout << "\t*Password must have at least 8 characters\n";
	cout << "\t*Password must consist of only letters and numbers\n";
	cout << "\t*Password must be at least 2 characters\n\n";
	getline(cin, inputPassword);
	
	if(inputPassword.length() < 8)
	{
		cout << "-Passsword must have at least 8 characters\n";
	}
	
	int numDigits = 0;
	for (int i = 0; i < inputPassword.length(); i++)
	{
		if (isdigit(inputPassword[i])) numDigits++;
	}
	if (isalnum(inputPassword.length()[i]))
	{
		cout << "Password can only contain letters and digits\n";
	}
	if (numDigits < 2)
	{
		cout << "Password must have at least 2 digits!\n";
	}

	return 0;
}
Nvm, just got it.
Topic archived. No new replies allowed.