Password always accepted.

I have to make a program that asks for someone to make up a password.

The password should at least contain:

* 6 characters.
* 1 uppercase letter.
* 1 lowercase letter.
* 1 number.

Maybe I'm not understanding the functions, because in my testing I always get the "Password Accepted message". Could someone help me out?

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>

using namespace std;
int testStr(char [], int);


int main ()
{
const int NUMBER = 100;

char string1[NUMBER];

cout<<"Make up a password that contains at least:\n\n";
cout<<"* 6 characters or more.\n";
cout<<"* 1 uppercase letter.\n";
cout<<"* 1 lowercase letter.\n";
cout<<"* 1 digit.\n\n";

cout<<"Enter password: ";
cin>>string1;
cout<<endl;

if(strlen(string1) <6)
{
cout<<"First test failed...further testing has stopped.\n";
cout<<"The password entered is less than 6 characters.\n\n";
          cout<<"Password should have included at least:\n\n";
          cout<<"* 6 characters or more.\n";
          cout<<"* 1 uppercase letter.\n";
          cout<<"* 1 lowercase letter.\n";
          cout<<"* 1 digit.\n\n";

system("pause");
exit(0);
}

int nx = strlen(string1);



switch(testStr(string1, nx))
{
case 1 :  cout<<"Password Accepted.\n";
          break;
case 2 :  cout<<"Second test failed.\n";
          cout<<"Uppercase letter not found in password.\n";
          cout<<"Further testing stopped.\n";
          break;
case 3 :  cout<<"Third test failed.\n";
          cout<<"Lowercase letter not found in password.\n";
          cout<<"Further testing stopped.\n";
          break;
case 4 :  cout<<"Fourth test failed.\n";
          cout<<"Digit not found in password.\n";
          break;
}//end switch







cout<<endl;
system("pause");
return 0;
}//end of main function



//test string function()
int testStr(char array[], int number)
{
    bool test2 = false;
    bool test3 = false;
    bool test4 = false;
     for(int counter = 0; counter < number; counter++)
     {
             //test uppercase letter
             if(isupper(array[counter]))
             {
             test2 = true;
             }
     }//end for loop

     for(int counter = 0; counter < number; counter++)
     {
             //test lowercase letter
             if(islower(array[counter]))
             {
             test3 = true;
             }

     }//end for loop

     for(int counter = 0; counter < number; counter++)
     {
             //test digit
             if(isdigit(array[counter]))
             {
             test4 = true;
             }
     }//end for loop}

if( test2 && test3 && test4)
return 1;

else if(test2 == false)
return 2;

else if(test3 == false)
return 3;

else if(test4 == false)
return 4;

}//end of testStr () 
Last edited on
Line 41 you are definitely meaning to pass in the number of characters the user actually entered, not the maximum length of the buffer.
But how would I know how long the password entered will be?


EDIT: Ok I fixed that part you pointed out jsmith and everything works now. I'm sorry for such an idiot question. I need to be more careful on such things.

Thank you.
Last edited on
The teststr() function doesn't need to take NUMBER -- the string is always terminated by a null character ('\0').

For example, line 77 could be written as one one of the following:

77 for (counter = 0; counter < strlen(array); counter++)

77 for (counter = 0; array[counter]; counter++)

However, I don't know why you aren't just using a std::string for user input:
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
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int testStr(const string&);


int main ()
{
    string string1;

    cout<<"Make up a password that contains at least:\n\n";
    cout<<"* 6 characters or more.\n";
    cout<<"* 1 uppercase letter.\n";
    cout<<"* 1 lowercase letter.\n";
    cout<<"* 1 digit.\n\n";

    cout<<"Enter password: ";
    getline(cin, string1);

    if(string1.length() < 6)
    {
        etc

Also, be careful about your indentation.

Hope this helps.
I believe the string1.length and other functions are in the next chapter. The programs are assigned after each chapter. I'm going to read that tomorrow and my class goes over that on Thursday.

Thanks though, everything in this place helps a lot.
Topic archived. No new replies allowed.