Password Verifier - Need Help!

I have tried just about everything I can think of, I keep getting runtime errors.
I am checking for alphabet first with "isalpha" before I add "isupper" & "islower" to my code.

i'm using visual studio.

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstring>
using namespace std;

//Function Prototype
void validatePassword(char[]);

const int SIZE = 30;

int main()
{
char password[SIZE];
int count = 0;


cout << "Enter your username and password to access your account.\n";
cout << "========================================"
<< "=======================================\n";
cout << "Must contain one digit.\n";
cout << "Must be at least 6 characters long.\n";
cout << "Must contain one Upper & one Lower case letter.\n";
cout << "Maximum of 30 characters.\n";
cout << "========================================"
<< "=======================================\n";
cout << "Username: JViviano\n";
cout << "Password: ";
cin >> (password);
cout << "========================================"
<< "=======================================\n";

cout << endl << "Your password is: ";
while (password[count] != '\0')
{
cout << password[count];
count++;
}
cout << endl;

validatePassword(password);

return 0;
}

void validatePassword(char password[SIZE])
{
bool length = false,
digit = false,
letter = false;

while ((length == false) ||
(digit == false) ||
(letter == false))
{

//Validate password length
if (strlen(password) >= 6)
{
length = true;
}

//Validate if password contains a digit
for (int i = 0; i < SIZE - 1; i++)
{
if (isdigit(password[i]))
{
digit = true;
}
}

//Validate if password contains letters
for (int count = 0; count < SIZE - 1; count++)
{
if (isalpha(password[count]))
{
letter = true;
}
}

cout << "The password is invalid:\n";

if (length == false)
{
cout << "Your Password is not at least 6 characters long.\n";
}
if (digit == false)
{
cout << "Your Password does not contain at least one digit.\n";
}
if (letter == false)
{
cout << "Your Password does not contain at least one lowercase\n"
<< " & one uppercase letter.\n";
}

cout << "========================================"
<< "=======================================\n";
cout << "Please Re-enter your password: ";
cin >> password;
}



if (digit = true)
{
cout << "digit is true\n";
}
if (letter = true)
{
cout << "letter is true\n";
}
if (length = true)
{
cout << "length is true.\n";
}
if ((length = true) &&
(letter = true) &&
(digit = true))
{
cout << "Valid Password.";
}
return;
}
Hi,

What kinds of runtime errors do you get?

ps: please use code tags and edit your code

http://www.cplusplus.com/articles/jEywvCM9/

pps: welcome to cplusplus.com :)
Write small functions.
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
#include <iostream>
#include <cstring>
#include <cctype>

bool has_digit( const char password[], std::size_t sz )
{
    for( std::size_t i = 0 ; i < sz ; ++i ) if( std::isdigit( password[i] ) ) return true ;

    return false ;
}

bool has_upper_case_letter( const char password[], std::size_t sz )
{
    for( std::size_t i = 0 ; i < sz ; ++i ) if( std::isupper( password[i] ) ) return true ;

    return false ;
}

bool has_lower_case_letter( const char password[], std::size_t sz )
{
    for( std::size_t i = 0 ; i < sz ; ++i ) if( std::islower( password[i] ) ) return true ;

    return false ;
}

bool error( const char* error_msg )
{
    std::cout << "*** error: " << error_msg << ".\n" ;
    return false ;
}

bool noisy_validate_password( const char password[] )
{
    if( password == nullptr ) return error( "null pointer" ) ;

    const std::size_t sz = std::strlen( password ) ;

    if( sz < 6 ) return error( "must be at least six characters long" ) ;
    if( sz > 30 ) return error( "can't be at more than 30 characters long" ) ;

    if( !has_digit( password, sz ) ) return error( "must contain a digit" ) ;
    if( !has_upper_case_letter( password, sz ) ) return error( "must contain an upper case letter" ) ;
    if( !has_lower_case_letter( password, sz ) ) return error( "must contain a lower case letter" ) ;

    return true ; // valid
}
This is the runtime error I've been getting.
I can't use multiple functions, just the one.
It is part of an assignment from school.
I just cant figure out what im doing wrong with 'isalpha' & 'isdigit'



Debug Assertion Failed!

Program: ...ments\Visual Studio 2015\Projects\P2 W3 A1\Debug\P2 W3
A1.exe
File: minkernel\crts\ucrt\src\appcrt\convert\isctype.cpp
Line: 36

Expression: c >= -1 && c <= 255

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)
= is assignment.
== is comparison.

You've forgotten that in a few places.

Do not limit your loops over password in your validation function to SIZE-1 characters, but strlen(password)-1 characters.

Do not get input in a function to validate a password.

Use [code][/code] tags when posting code.
Topic archived. No new replies allowed.