password verification problem

I have two questions.

First in both lines of this same code (44, 49):

length=strlen(password);

I get an error that says "implicit conversion loses integer precision"

What does this mean?

Also my second problem is that when I test this program it seems to work ok until I have run enough passwords through to turn each of the bools to true and then it will accept the next password I enter regardless of whether or not it actually fits the criteria.

example

PW entry #1: 1111111 (error about no upper case or lower case, but will turn the has_digits flag to true and it will stay that way regardless of subsequent pw entries)

#2: 11111A will set has_upper to true

#3: 11111a will set has_lower to true

#4 123 This password doesn't meet any of the criteria but will be accepted because all the flags have been tripped to true.

How to I reset the flags to false for every iteration of the loop?

Thanks

Adam

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 //
//  main.cpp
//  Password Validator
//
//  Created by Adam Windsor on 9/3/14.
//  Copyright (c) 2014 Adam Windsor. All rights reserved.
//

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
void validatePassword(char [ ]);
const int SIZE=100;

int main()
{
    
    char password[SIZE];
    cout << "Please enter a new password " << endl;
    cout << "The password should be at least 6 characters long. " << endl;
    cout << "The password should contain at least one uppercase and at least one lowercase letter. " << endl;
    cout << "The password should have at least one digit. " << endl;
    
    validatePassword(password);
    
    
    
    
    return 0;

}

void validatePassword(char *password)
{
    int length;
    
   
    bool has_uppers = false, has_lowers = false, has_digits = false;

    do
    {
        cin.getline(password, SIZE);
        length=strlen(password);
        while (length < 6)
        {
            cout << "Please enter at least six characters " << endl;
            cin.getline(password, SIZE);
            length=strlen(password);
        }
        
        
        
        //int length = strlen(password);
        for( int i=0; i<length; i++)
        {
            char ch = password[i];
            has_uppers |= isupper(ch);
            has_lowers |= islower(ch);
            has_digits |= isdigit(ch);
            
        }
        
        
        
        
        if (has_uppers == false)
        {
            cout << "Password must contain an upper case letter." << endl;
        }
        if (has_lowers == false)
        {
            cout << "Password must contain a lower case letter" << endl;
        }
        if (has_digits == false)
        {
            cout << "Password must contain a number" << endl;
        }
        
        if (has_uppers == false || has_lowers == false || has_digits == false)
        {
            cout << "please re-enter password" << endl;
            
        }
        
    } while (has_uppers == false || has_lowers == false || has_digits == false);
    cout << password << endl;
}
First in both lines of this same code (44, 49):

length=strlen(password);

I get an error that says "implicit conversion loses integer precision"

What does this mean?

It means that you're using the wrong type of variable to hold the return value from strlen(). This function doesn't return an int it returns a size_t so length should be a size_t not an int.

How to I reset the flags to false for every iteration of the loop?

Are you going to use these variables after you are finished with the loop? If not then why not declare them inside the loop body. Keep your variables as local as possible.
Topic archived. No new replies allowed.