#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    C++ Help please program that requests a password


    I am having trouble on this program it is passed due I have been working on it for a while could someone please look at what I have and help me correct it?

    program that requests a password and verifies that it is a valid
    password. To be valid the password must be at least 6 characters long and
    contain at least one digit.

    Sample Output (inputs in bold)
    Please enter a password: pass6
    Passwords must be at least 6 characters long
    Please enter a password: TarrantNW
    Passwords must include at least one digit (1-9)
    Please enter a password: Tccd03
    Thank you, that is a valid password

    #include <iostream>
    #include <ctype.h>
    #include <cctype>
    #include <string>
    #include <stdio.h>

    using namespace std;

    char password[6];

    const int SIZE = 6;

    bool verifypass(char *userinput,bool valid)

    //password verification function

    {
    valid = (0 == strcmp (userinput,password));

    if (valid)
    {
    cout << "Thanks. That is a valid password." << endl;
    }
    else
    {
    cout << "Invalid password" << endl;
    }


    return 0;
    }


    int verify_chars(char *userinput,bool valid)

    //verifies how many characters
    {
    int length;

    length = strlen(userinput);

    //checks for how many characters

    if ((length <= 6) && (length >= 6))
    {
    valid=true;
    }
    else
    {
    valid =false;
    cout << "The password must be at least 6 characters long" << endl;
    }
    return valid;
    }

    int main()
    {
    bool valid,upper;

    char userinput[1024];
    cout << "Please enter your password: " << endl;
    cin >> userinput;


    // This will test input for digits in the string

    bool digit_yes=false;

    int num = 0;
    int len = strlen(userinput);
    for (int count = 0; count < len; count++)

    if (isdigit(userinput[count]))
    digit_yes=true;

    if (!digit_yes)
    {
    valid=false;
    cout <<"This passworde has to have at least one digit (1-9)"<< endl;
    }
    else
    {
    valid=true;
    }



    // this function will test the number of characters

    verifypass(userinput,valid);



    return 0;

    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    45
    Rep Power
    8
    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.\n";
            return true;
        }
        else
        {
            cout << "Invalid password\n";
            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\n";
            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)\n";
        return false;
    }
    
    int main()
    {
        char userinput[1024];
        cout << "Please enter your password:\n";
        cin >> userinput;
        if (verify_chars(userinput) == true)
        {
            verify_pass(userinput);
        }
        return 0;
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    thanks


    thanks very much!

IMN logo majestic logo threadwatch logo seochat tools logo