Password Validation Program Help

Hey there. I have a coding assignment I'm supposed to complete for my Intro to C++ class.

I thought I had everything figured out, but for some reason, my code isn't working. It keeps giving incorrect answers.

The specifications for the program are as follows:

1. The password must contain at least one uppercase letter, one lowercase letter and a number.
2. It can’t have any punctuation character, accent or space.
3. The password must be 6 to 32 characters.

Input
The input contains several test cases and ends with EOF. Each line has a string S, corresponding to the password that is entered by the user at registration.

Output
The output contains a line, which can be "Senha valida.", if the password has all previously requested requirements, or "Senha invalida.", if one or more requirements aren’t met.


Here is the code I currently have:
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
#include <iostream>
#include <string>
#include <ctype.h>
#include <cmath>
#include <cstdlib>


using namespace std;

int main()
{
    string password;
    int b;
    bool length=false, badChar=true, upper=false, lower=false, num=false;

    while(getline(cin, password)){
            b=password.length();
        if(b <= 32 && b >=6){
            length=true;
        }

        for(int a=0; a<1; a++){
            if ((password[a]>=97 && password[a]<=122) || (password[a]>=65 && password[a]<=90) || (password[a]>=48 && password[a]<=57)){
                badChar=false;
            }
            if (password[a]>=48 && password[a]<=57){
                num=true;
            }
            if (password[a]>= 65 && password[a]<=90){
                upper=true;
            }
            if (password[a]>=97 && password[a]<=122){
                lower=true;
            }

        }
        if(length==true && badChar==false && num==true && upper==true && lower==true){
            cout << "Senha valida." << endl;
        }
        else {
            cout << "Senha invalida." << endl;
        }

    }



    return 0;
}


Is there anyway you all could tell me what is broken/wrong with the code? Thanks in advance.
Last edited on
describe what it is doing wrong?

I suspect your problem is the badchar part.
if, for example you have a 7 letter password and letter #1 is 'bad'. Nothing sets badChar to true. Then letter 2 is valid and badchar is false and it passes your tests.

you probably need to reverse the logic and if it finds a bad char, set that to true, and initialize badchar to false, as you did with the others.
Last edited on
It just isn't giving the right answer.

Currently, its saying that it is always invalid.

I tried to do what I think is reverse logic. I'm still very new to all of this. We haven't done anything like this in class, really.

Here is what I have now:

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
#include <iostream>
#include <string>
#include <ctype.h>
#include <cmath>
#include <cstdlib>


using namespace std;

int main()
{
    string password;
    int b;
    bool length=false, badChar=false, upper=false, lower=false, num=false;

    while(getline(cin, password)){
        length = false;
        badChar = false;
        upper = true;
        lower = true;
        num =true;
            b=password.length();
        if(b <= 32 && b >=6){
            length=true;
        }

        for(int a=0; a<1; a++){
            if (!((password[a]>=97 && password[a]<=122) || (password[a]>=65 && password[a]<=90) || (password[a]>=48 && password[a]<=57))){
                badChar=true;
            }
            if (!(password[a]>=48 && password[a]<=57)){
                num=false;
            }
            if (!(password[a]>= 65 && password[a]<=90)){
                upper=false;
            }
            if (!(password[a]>=97 && password[a]<=122)){
                lower=false;
            }

        }
        if(length==true && badChar==true && num==false && upper==false && lower==false){
            cout << "Senha valida." << endl;
        }
        else {
            cout << "Senha invalida." << endl;
        }
        cout << b << endl;

    }



    return 0;
}
Last edited on
you have an = instead of == in that condition.
also, only the first one was incorrect before.
Okay, so just 'fixing' the first one doesn't change anything. I'm still getting all invalids. Unless I'm not fixing the first one correctly?


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
#include <iostream>
#include <string>
#include <ctype.h>
#include <cmath>
#include <cstdlib>


using namespace std;

int main()
{
    string password;
    int b;
    bool length=false, badChar=false, upper=false, lower=false, num=false;

    while(getline(cin, password)){
        length = false;
        badChar = false;
        upper = false;
        lower = false;
        num =false;

            b=password.length();
        if(b <= 32 && b >=6){
            length=true;
        }

        for(int a=0; a<1; a++){
            if (!(password[a]>=97 && password[a]<=122) || !(password[a]>=65 && password[a]<=90) || !(password[a]>=48 && password[a]<=57)){
                badChar=true;
            }
            if (password[a]>=48 && password[a]<=57){
                num=true;
            }
            if (password[a]>= 65 && password[a]<=90){
                upper=true;
            }
            if (password[a]>=97 && password[a]<=122){
                lower=true;
            }

        }
        if(length==true && badChar==true && num==true && upper==true && lower==true){
            cout << "Senha valida." << endl;
        }
        else {
            cout << "Senha invalida." << endl;
        }

    }



    return 0;
}
you need to debug this carefully.

change it to check 1 condition at a time, and then test that condition on both the true and false setups. Do the simple ones first, and work up to the complex one.

for the complex letter tester, be sure to check A, a, z, Z, 0, 9, and any other edge cases.

I am at work now and can look again later. I was going to try to find the issue but I upended my drink on my PC when I was looking at it last night and had to turn everything off to dry out.
Last edited on
Topic archived. No new replies allowed.