homework due tomorrow...cannot make it run properly

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?
This is the question

(Check password, string computation, and loop) Some websites impose certain rules for
passwords. Suppose the password rules are as follows:
 A password must have at least eight characters.
 A password must consist of only letters and digits.
 A password must contain at least two digits.
Write a program that prompts the user to enter a password and display “Valid Password” if the
rules are followed or “Invalid Password” otherwise.
I recommend you to create a function that verifies the password, just to clean up main().
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
bool verify_password( const string& password )
{
    // give magic numbers a name
    const size_t MIN_LENGTH{ 8 };

    if( password.length( ) < MIN_LENGTH ) {
        cout << "Your password must be at least 8 characters\n";
        return false;
    }

    // ...
}

int main( )
{
    string password{};
    cout << "Enter a password: ";
    cin >> password;

    if( !verify_password( password ) )
        return 1;   // exit program

    else
        // password correct
}



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
1
2
3
if ((isdigit(ch)) || (isalpha(ch)))
    correctPassword = true;
cout << "You have entered an invalid character.\n";

Have you assigned anything to ch? What does ch hold?

Double post: http://www.cplusplus.com/forum/beginner/202967/
Last edited on
Ok...
Thank you
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
bool verify_password( const string& password )
{
    // give magic numbers a name
    const size_t MIN_LENGTH{ 8 };

    if( password.length( ) < MIN_LENGTH ) {
        cout << "Your password must be at least 8 characters\n";
        return false;
    }

    // ...
}

int main( )
{
    string password{};
    cout << "Enter a password: ";
    cin >> password;

    if( !verify_password( password ) )
        return 1;   // exit program

    else
        // password correct
}


So this replaces the first half of what I wrote?

As for the assigning to ch....
I have changed it three different ways....I am just not understanding it.

I am so new to this, I don't always understand what it is someone is suggesting...sorry
I'm not forcing you to do it my way, it's just that it's easier to solve a problem if you break it down into smaller problems. That's why we have functions that each do their own thing, so as to solve each sub-problem.

Just code whatever way works for you.

Anyway, your issue is here:
1
2
3
if ((isdigit(ch)) || (isalpha(ch)))
    correctPassword = true;
cout << "You have entered an invalid character.\n";

ch doesn't hold any meaningful value. It isn't actually a character of Password, because you haven't told it to be.

1
2
3
4
5
6
7
8
9
10
11
12
for (int i = 0; i < Password.length(); i++)
{
    // store the current character
    ch = Password[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";

    // ...
}
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/202967/
Ok. I understand that, and it fixed part of the problem.

1)The code still does not keep looping until it validates the password as correct.
2) it is not recognizing the numbers as numbers...even if i enter two numbers, it still says I need two numbers.
3) if I enter, for example, a # in the password, it just says I need two numbers, not that I have entered an invalid character

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
68
69
70
//
//Assignment 4 Q5
//Password validation with: 
// at least 8 characters
// at least two digits
// no characters other than letters or digits

#include<iostream>
#include <string>

using namespace std;

int main()
{

	string Password;
	bool correctPassword = false;
	char ch = 0;
	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++)
			{
				// store the current character
				ch = Password[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 ";
		//}

	}

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

	//end program 
	return 0;
}
Last edited on
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
#include <iostream>
#include <string>
#include <cctype>

bool has_only_letters_and_digits( std::string str )
{
    for( char c : str ) if( !std::isalnum(c) ) return false ;
    return true ;
}

std::size_t num_digits( std::string str )
{
    std::size_t cnt = 0 ;
    for( char c : str ) if( std::isdigit(c) ) ++cnt ;
    return cnt ;
}

bool valid_password( std::string str )
{
    return str.size() > 7 && // must have at least eight characters
           has_only_letters_and_digits(str) && // must consist of only letters and digits
           num_digits(str) > 1 ; // must contain at least two digits
}

int main()
{
    bool valid ;

    do
    {
        std::cout << "enter password: " ;
        std::string password ;
        std::cin >> password ;

        valid = valid_password(password) ;

        if(!valid) std::cout << "invalid password. try again\n" ;

    } while( !valid ) ;

    std::cout << "valid password.\n" ;
}
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string password;
    size_t size = 0;
    int no_digits = 0;
    
    bool correct_password = true;
    
    do
    {
        correct_password = true;
        
        cout << "Enter password: ";
        cin >> password;
        
        size = password.length();
        
        // CHECK LENGTH
        if( size < 8 )
        {
            cout << "Password must contain 8 or more characters\n";
            correct_password = false;
        }
        
        // CHECK ALL ALPHANUMERIC
        for(size_t i = 0; i < size; i++)
        {
            if ( !isalnum(password[i]))
            {
                cout << "Password contains non alphanumeric character\n";
                correct_password = false;
            }
        }
        
        // CHECK NUMBER OF DIGITS
        for(size_t i = 0; i < size; i++)
        {
            if ( isdigit(password[i]))
            {
                no_digits++;
            }
        }
        if (no_digits < 2)
        {
            cout << "Password contains less than 2 digits\n";
            correct_password = false;
        }
        
        cout << "Password ";
        if( correct_password)
            cout << "is ";
        else
            cout << "is not ";
        cout << "acceptable\n";
        
    } while( correct_password == false);
    
    return 0;
}
Topic archived. No new replies allowed.