Thread: Homework Help

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

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Homework Help


    Hello everyone I been trying to do this for the past few with no luck could someone help me?

    I have to write a code that will request a password and validate it.
    It needs to be 7 characters long, has a digit or a $.

    Here is what I have so far but whenever it asks for a password with 7 characters it closes the window, and I don't know where I should write the requirement for a "$".

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

    using namespace std;

    const int SIZE = 7;
    char password[SIZE + 1];

    bool verify_pass(const char *userinput)
    {if (strcmp(userinput, password) == 0)

    {cout << "Thank you, that is a valid password.\n";
    return true;}
    else{
    cout << "Passwords must be at least 7 characters long\n";
    return false;}}
    bool verify_chars(const char *userinput)
    {int i, len = strlen(userinput);
    if (len < SIZE)
    {cout << "Passwords must be at least " << SIZE << " characters long\n";
    return false;
    }

    for (i = 0; i < len; ++i)
    {
    if (isdigit(userinput[i]))
    {return true;}
    }
    cout << "Passwords must include a digit or dollar sign (0-9, $)\n";
    return false;}

    int main()

    {char userinput[1024];
    cout << "Please enter a password:\n";
    cin >> userinput;
    if (verify_chars(userinput) == true)
    {verify_pass(userinput);}
    return 0;}
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    How about including a test for the dollar sign character?

    if (isdigit(userinput[i]) || ('$' == userinput[i]))

    Comments on this post

    • codergeek42 agrees : Random: Good idea to put the string literal first when testing equality, to help against accidental assignments instead of "is equal" testing.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0
    Also whenever I try to debug it, i type a random password into and it tells me that i need a 7 character password but after that it won't ask me to type in a password again and just closes.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    That's because there's no loop around
    gather input
    verify characters


    do cin>>userinput;while(!verify_chars(userinput));
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0
    Sorry, where would I put that inbetween? and is it necessary for me to have two "Passwords must be at least 7 characters long\n"; in the coding?
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Warning: because of my dreadful c++ knowledge the details of this post may be entirely wrong. (In particular, I don't know const precedence nor left-rightness, and I'm not going to try to understand now.)

    If you need to repeat a value and you're using c++ you should assign that value to a constant.

    Code:
    const char password_length_message[] = "Passwords must be at least 7 characters long";
    //...
    puts(password_length_message);
    //...
    puts(password_length_message);
    In a prior post, this thread, I suggested

    do statement;
    while (condition);

    Will this not work?

    Sometimes I need to repeat statements before and within a loop, always thinking it's a particular defect of mine.

    Also consider what the interaction should be. Are the messages tiresome for the user who understands the requirement but carelessly whacked both the \ and enter keys together for the forth time? What's acceptable. Do you really need the person's name so you can repeat it back? Do you care if the user uses slashes dashes or spaces in the credit card number? Can't the programmer simply filter them away?

    Should I have to click through 5 selections to perform a daily mandatory task? Should Oracle software present me with a word that's the wrong part of speech every day when I finish that task????
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Warning: because of my dreadful c++ knowledge the details of this post may be entirely wrong. (In particular, I don't know const precedence nor left-rightness, and I'm not going to try to understand now.)

    If you need to repeat a value and you're using c++ you should assign that value to a constant.

    Code:
    const char password_length_message[] = "Passwords must be at least 7 characters long";
    //...
    puts(password_length_message);
    //...
    puts(password_length_message);
    In a prior post, this thread, I suggested

    do statement;
    while (condition);

    Will this not work?

    Sometimes I need to repeat statements before and within a loop, always thinking it's a particular defect of mine.

    Also consider what the interaction should be. Are the messages tiresome for the user who understands the requirement but carelessly whacked both the \ and enter keys together for the forth time? What's acceptable. Do you really need the person's name so you can repeat it back? Do you care if the user uses slashes dashes or spaces in the credit card number? Can't the programmer simply filter them away?

    Should I have to click through 5 selections to perform a daily mandatory task? Should Oracle software present me with a word that's the wrong part of speech every day when I finish that task????
    Well what's suppose to happen is I type in "pass6" as apassword the first time and its suppose to say that the password needs to be 7 characters in length and then the 2nd time im suppose to type TarrantNW and it suppose to tell me that my password must require atleast a digit or $. and last i must type in Tccd-03 and it should accept the password.
    "Sample Output (inputs in bold)
    Please enter a password: pass6
    Passwords must be at least 7 characters long
    Please enter a password: TarrantNW
    Passwords must include a digit or dollar sign (0-9, $)
    Please enter a password: Tccd-03
    Thank you, that is a valid password"

    and I remember that you told me to put "cin>>userinput;while(!verify_chars(userinput));" but I was wondering which line/lines I put it on.
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    You'd need to figure out what it does, and if the statement is useful, where it would fit. I spent some time with that issue today debugging percolation simulation. I first wrote the j solution, with which I had no trouble.
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0
    This is frustrating I have no idea where I should put the loop.
  18. #10
  19. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,648
    Rep Power
    1945
    Originally Posted by ehuynh
    This is frustrating I have no idea where I should put the loop.
    You want to put it around the code that ask and validate the user input/password.
    And then you have to find out how to get out of the loop, when the pass is ok.

    I would place the call verify_chars inside verify_pass in this case.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0
    Could you show me what it would should look like please?

IMN logo majestic logo threadwatch logo seochat tools logo