Strings in C++

Hello,
I am trying to write a program that takes in a password and test it to make sure that it is at least 7 characters long and contains a digit 0-9 or a dollar sign. I can not seem to get it to work, i think there may be a few problems or maybe I am missing something. Any help is greatly appreciated, thank you! Below is my source code.
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
#include <iostream>
#include <string>
using namespace std;


int main()
{
	string password_var;
	int i=1;
	bool validPass;
	int Length_Of;

	while (1)
	{
		cout << "Please enter a password.\n";
		cin >> password_var;
		validPass = false;
		Length_Of = password_var.size();
		if (Length_Of >= 7)
		{
			cout << "Passwords must be at least 7 characters long.\n";
		}
			while (1)
			{
				if (password_var[i] >= '0'&&password_var[i] <= '9') || (password_var[i] = '$')
				{
					validPass = true;
				}
				else
				{
					i = i + 1;
				}
				if (validPass = true || i > Length_Of)break;
			}
			if (validPass == true)
			{
				cout << "Thank you, that is a valid password.\n";
			else
			{
				cout << "Passwords must include a digit or a dollar sign(0-9,$).\n";
			}
			if (validPass == true)break;
			}
			return 0;
		}
In general for such a requirement, you should use regular expressions.

Regarding your code, there are multiple logical errors.

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


int main()
{
    string password_var;
    bool validPass;
    int Length_Of;
    
    while (1)
    {
        cout << "Please enter a password.\n";
        cin >> password_var;
        validPass = false;
        Length_Of = password_var.size();
        if (Length_Of < 7)
        {
            cout << "Passwords must be at least 7 characters long.\n";
            continue; // you don't need to go further in the loop.
        }
        
        // initialize i here.
        int i = 0;
        while (1)
        {
        	
        	// beware of assignment '=' and comparison '=='
            if ( (password_var[i] >= '0'&&password_var[i] <= '9') 
                 || (password_var[i] == '$')
        		)
            {
                validPass = true;
            }
            else
            {
                i = i + 1;
            }
            // beware of assignment '=' and comparison '=='
            if (validPass == true || i >= Length_Of) 
                break;
        }
        if (validPass == true)
        {
            cout << "Thank you, that is a valid password.\n";
        } // add the closing brace.
        else
        {
            cout << "Passwords must include a digit or a dollar sign(0-9,$).\n";
        }
        if (validPass == true)
            break;
    }
    return 0;
}


http://ideone.com/4pN7rV
Last edited on
In general for such a requirement, you should use regular expressions.
normally i would agree, and they are great to learn anyways, but in this case it might be better to do something like this:

1
2
3
4
5
6
7
8
9
10
11
bool validPassword(std::string Password, int MaxLength=7)
{
    if(Password.length() < 7)
        return false;

    for(char Iterator : Password)
        if(isdigit(Iterator) || Iterator == '$')
            return true;

    return false;
}
@Little Bobby Tables
Why isn't regex suitable for this case? IMO a simple regex with lookahead shall do the job.
Last edited on
simple regex

i dont believe you ;)
/joke

in my opinion regexs are for parsing large amounts of data. mine is also easier or as easy to implement. finally, unless it was fixed, <regex> isnt fully supported by all compilers yet (namely gcc) so you would have to use a third party one. but let me repeat for op's sake: you should still learn regexs. they are a very valuable tool
Topic archived. No new replies allowed.