Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to write a program in C++ that will verify a user has entered a pass

ID: 3545983 • Letter: I

Question

I am trying to write a program in C++ that will verify a user has entered a password that is at least 6 characters long and has at least 1 digit. I keep getting invalid password. Here is my cource code;


#include <iostream>

#include <cstring>

#include <cctype>


using namespace std;


const int SIZE = 6;

char password[SIZE + 1]; // one more byte for terminating character


bool verify_pass(const char *userinput)

{

    if (strcmp(userinput, password) == 0)

    {

        cout << "Thanks. That is a valid password. ";

        return true;

    }

    else

    {

        cout << "Invalid password ";

        return false;

    }

}


bool verify_chars(const char *userinput)

{

    int i, len = strlen(userinput);

    if (len < SIZE)

    {

        cout << "The password must be at least " << SIZE << " characters long ";

        return false;

    }

    for (i = 0; i < len; ++i)

    {

        if (isdigit(userinput[i]))

        {

            return true;

        }

    }

    cout << "This password has to have at least one digit (0-9) ";

    return false;

}


int main()

{

    char userinput[1024];

    cout << "Please enter your password: ";

    cin >> userinput;

    if (verify_chars(userinput) == true)

    {

        verify_pass(userinput);

    }

    return 0;

}

Explanation / Answer

it is because you haven't initialize the CHARACTER ARRAY   password[size+1] so when ever it check the

bool verify_pass(const char *userinput){

if (strcmp(userinput, password) == 0){   //    this get false and
cout << "Thanks. That is a valid password. ";
return true;
}

else{
cout << "Invalid password ";              //   print these statement
return false;
}

}


//                    so initialize the password[SIZE + 1]   first then check the validity


#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

const int SIZE = 6;
char password[SIZE + 1]; // one more byte for terminating character

bool verify_pass(const char *userinput){

if (strcmp(userinput, password) == 0){

cout << "Thanks. That is a valid password. ";
return true;

}

else{
cout << "Invalid password ";
return false;

}

}


bool verify_chars(const char *userinput){

int i, len = strlen(userinput);
if (len < SIZE){
cout << "The password must be at least " << SIZE << " characters long ";
return false;
}

for (i = 0; i < len; ++i){

if (isdigit(userinput[i])){
return true;
}

}

cout << "This password has to have at least one digit (0-9) ";

return false;

}


int main(){

char userinput[1024];
cout << "Please enter your password: ";
cin >> userinput;
if (verify_chars(userinput) == true){

verify_pass(userinput);

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote