Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

password program

RaizoRaizo Posts: 131Member
Hi,
I created a program that uses a loop to have the user enter a password, cover the password by "*" 's and check the password for validity. My question is how can the program exit the loop when the user presses enter?

[code]#include
#include
using namespace std;

int main()
{
int j;
char c[80];

cout << "Enter password: ";

for(j=0; ; j++ ){
c[j] = getch();
[green] //something here? [/green]
putch('*');
}

if(!strcmp(c, "password")) cout << "Password accepted.
";
else cout << "Access denied.
";

return 0;
}[/code]

Thanks for any help at all
«1

Comments

  • whoiewhoie Posts: 672Member
    : Hi,
    : I created a program that uses a loop to have the user enter a password, cover the password by "*" 's and check the password for validity. My question is how can the program exit the loop when the user presses enter?
    :
    : [code]
    : #include
    : #include
    : using namespace std;
    :
    : int main()
    : {
    : [red]int c;[/red]
    : int j;
    : char pw[80];
    :
    : cout << "Enter password: ";
    :
    : for(j=0;[red]j < 79 && (c = getch()) != '
    '[/red]; j++ ){
    : pw[j] = [red]c[/red];
    : putch('*');
    : }
    :
    : [red]pw[j] = '';[/red]
    : if(!strcmp(pw, "password")) cout << "Password accepted.
    ";
    : else cout << "Access denied.
    ";
    :
    : return 0;
    : }[/code]

    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • RaizoRaizo Posts: 131Member
    : : Hi,
    : : I created a program that uses a loop to have the user enter a password, cover the password by "*" 's and check the password for validity. My question is how can the program exit the loop when the user presses enter?
    : :
    : : [code]
    : : #include
    : : #include
    : : using namespace std;
    : :
    : : int main()
    : : {
    : : [red]int c;[/red]
    : : int j;
    : : char pw[80];
    : :
    : : cout << "Enter password: ";
    : :
    : : for(j=0;[red]j < 79 && (c = getch()) != '
    '[/red]; j++ ){
    : : pw[j] = [red]c[/red];
    : : putch('*');
    : : }
    : :
    : : [red]pw[j] = '';[/red]
    : : if(!strcmp(pw, "password")) cout << "Password accepted.
    ";
    : : else cout << "Access denied.
    ";
    : :
    : : return 0;
    : : }[/code]
    :
    : HTH,
    : Will
    : --
    : http://www.tuxedo.org/~esr/faqs/smart-questions.html
    : http://www.eskimo.com/~scs/C-faq/top.html
    : http://www.parashift.com/c++-faq-lite/
    : http://www.accu.org/
    :
    :
    :

    Thanks for the changes, but it still doesn't seem to work. . . I copied it exactly as it appears here, but when I press enter, more asterisks appear as if I am pressing just another letter. In case it matters, I am using Microsoft Visual C++ 6.0.
  • whoiewhoie Posts: 672Member
    : Thanks for the changes, but it still doesn't seem to work. . . I copied it exactly as it appears here, but when I press enter, more asterisks appear as if I am pressing just another letter.

    Well, do a little experiment, and see what the return value of 'getch' is when you press enter. Perhaps it is '
    ' instead of the standard '
    '.


    : In case it matters, I am using Microsoft Visual C++ 6.0.

    Can't help you there.


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • RaizoRaizo Posts: 131Member
    : : Thanks for the changes, but it still doesn't seem to work. . . I copied it exactly as it appears here, but when I press enter, more asterisks appear as if I am pressing just another letter.
    :
    : Well, do a little experiment, and see what the return value of 'getch' is when you press enter. Perhaps it is '
    ' instead of the standard '
    '.
    :
    :
    : : In case it matters, I am using Microsoft Visual C++ 6.0.
    :
    : Can't help you there.
    :
    :
    : HTH,
    : Will
    : --
    : http://www.tuxedo.org/~esr/faqs/smart-questions.html
    : http://www.eskimo.com/~scs/C-faq/top.html
    : http://www.parashift.com/c++-faq-lite/
    : http://www.accu.org/
    :
    :
    :

    Thank you very much for your help. It was '
    ' instead of '
    '. There is only one thing I still don't understand. The only way I was able to get the break to work was to add an "if" statement within the "for" loop. The conditional statement in the expression part of the "for" loop didn't seem to work. Do you know why this is? It might not be very important, but I was just thinking it might be useful information for future use. If MSVC++ is not efficient, like you make it seem, I've only been using C++ for a very short amount of time and I don't understand enough of what I am doing to understand its problems! Well, thanks again for the help!
  • whoiewhoie Posts: 672Member
    : Thank you very much for your help. It was '
    ' instead of '
    '. There is only one thing I still don't understand. The only way I was able to get the break to work was to add an "if" statement within the "for" loop. The conditional statement in the expression part of the "for" loop didn't seem to work. Do you know why this is?

    Without seeing your code, no. This seems to work like you want on my MSVC++ 6.0:
    [code=ffffff]
    #include
    #include
    #include // for strcmp

    using namespace std;

    int main(void)
    {
    int c;
    int i;
    char pw[80];

    cout << "Enter Password: ";

    for ( i = 0; i < 79 && (c = getch()) != '
    '; ++i ) {
    pw[i] = c;
    putch('*');
    }

    pw[i] = '';

    cout << endl;

    if ( strcmp(pw, "password") == 0 ) {
    cout << "Correct" << '
    ';
    } else {
    cout << "Incorrect" << '
    ';
    }

    return 0;
    }
    [/code]


    : It might not be very important, but I was just thinking it might be useful information for future use. If MSVC++ is not efficient, like you make it seem,

    If I gave that impression, then I am truly sorry. I had no intention of doing so. I simply don't use that compiler for anything. Considering the time period it was developed, the number of users, and amount of code compiled on that compiler, it should do you just fine. Sure, there are problems with it, but I have yet to use a compiler that is perfect.


    : I've only been using C++ for a very short amount of time and I don't understand enough of what I am doing to understand its problems! Well, thanks again for the help!

    If I were you, I would forgo the use of non-standard functions (i.e. getch). Learn the core language first, then learn extensions to it. That way, you will know the difference between a language issue, and an implementation issue. Once you can make that distinction, you won't be a newbie anymore.


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • BajanineBajanine Posts: 12Member
    [b][red]This message was edited by Bajanine at 2002-11-2 2:21:10[/red][/b][hr]
    : : Hi,
    : : I created a program that uses a loop to have the user enter a password, cover the password by "*" 's and check the password for validity. My question is how can the program exit the loop when the user presses enter?
    : :
    : : [code]
    : : #include
    : : #include
    : : using namespace std;
    : :
    : : int main()
    : : {
    : : [red]int c;[/red]
    : : int j;
    : : char pw[80];
    : :
    : : cout << "Enter password: ";
    : :
    : : for(j=0;[red]j < 79 && (c = getch()) != '
    '[/red]; j++ ){
    : : pw[j] = [red]c[/red];
    : : putch('*');
    : : }
    : :
    : : [red]pw[j] = '';[/red]
    : : if(!strcmp(pw, "password")) cout << "Password accepted.
    ";
    : : else cout << "Access denied.
    ";
    : :
    : : return 0;
    : : }[/code]
    :
    : HTH,
    : Will
    : --
    : http://www.tuxedo.org/~esr/faqs/smart-questions.html
    : http://www.eskimo.com/~scs/C-faq/top.html
    : http://www.parashift.com/c++-faq-lite/
    : http://www.accu.org/
    :
    :
    :

    I compiled and ran the previous example with backslash-r and it worked correctly for me. I use MSVC++6 and Win XP/Win2k/Win98. Just for grins here is a simple pass function I wrote a few years ago.
    /* Password function. */
    /* 09-20-00 */
    /* Non-ANSI listing */
    #include <stdio.h>
    #include
    #include
    #include

    // Be sure to go into the project settings and add winmm.lib to the link files box if you plan on using the playsound function.



    int GetPassWord();


    int main()
    {
    printf("
    Enter Password: ");
    GetPassWord();
    return 0;
    }



    int GetPassWord()
    {
    int a = 0;
    int b = 0;
    int c = 0;

    char *SeeMe = "*";
    char PassWord[128] = {''};
    char PassLetter;

    do
    {
    do
    {
    PassLetter = getch();
    printf("%s", SeeMe);
    {
    /* put Enter password here to look normal!!! */
    PassWord[a] = PassLetter;
    if(PassLetter == '
    ') /* if is enter key store null */
    PassWord[a] = '';
    a++;
    if(a == 127) /* if to many letters start over at zero with out indication */
    {
    a = 0;
    PassWord[0] = ''; /* clear previous input */
    }
    }
    }while(PassLetter != '
    ');
    b = (strcmp(PassWord, "pass"));
    a=0; /* Reset array loop to zero 1st letter of password. */
    if(b != 0)
    {
    printf("
    Invalid password, try again. ");
    PlaySound("InvalidPassWordTryAgain.wav",NULL,SND_SYNC); // Project|Settings|link files| add 'winmm.lib'
    }
    }while(b != 0);
    printf("
    Correct!
    ");
    PlaySound("accessgranted.wav",NULL,SND_SYNC); // Project|Settings|link files| add 'winmm.lib'
    return 0;
    }



  • RaizoRaizo Posts: 131Member
    : If I were you, I would forgo the use of non-standard functions (i.e. getch). Learn the core language first, then learn extensions to it. That way, you will know the difference between a language issue, and an implementation issue. Once you can make that distinction, you won't be a newbie anymore.
    :

    Judging by how long it took me to figure out how to make the program work, I am definitely going to take your advice. Does the core language, as you call it, include "cout" and "cin"? I've read these are better than using "printf()" and "scanf()". Would you suggest using "cout" and "cin" rather than the others? If so, is there an input command similar to getch() (not displaying the character on the screen) using "cin" or some variation? Thanks for your help!
  • whoiewhoie Posts: 672Member
    : : If I were you, I would forgo the use of non-standard functions (i.e. getch). Learn the core language first, then learn extensions to it. That way, you will know the difference between a language issue, and an implementation issue. Once you can make that distinction, you won't be a newbie anymore.
    : :
    :
    : Judging by how long it took me to figure out how to make the program work, I am definitely going to take your advice.

    Great! :)


    : Does the core language, as you call it, include "cout" and "cin"?

    Yes.


    : I've read these are better than using "printf()" and "scanf()". Would you suggest using "cout" and "cin" rather than the others?

    This is a frequently asked question, so you should consult the FAQ for the answer. It is the third link in my signature. Short answer: yes.


    : If so, is there an input command similar to getch() (not displaying the character on the screen) using "cin" or some variation?

    This is also a frequently asked question. Please consult the FAQ first. Short answer: no.


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • slotosloto Posts: 2Member
    This is another simple password program I just wrote. The master password is a const char array so you can change it to whatever you like. When the password is correct, the program keeps going.

    #include
    #include

    using namespace std;

    int main()
    {
    int valid;
    const char str1[] = "password";
    char str2[64];
    do
    {
    cout<<"Enter the password: ";
    cin>>str2;

    valid = strcmp(str1, str2);
    if(valid != 0)
    {
    cout<<"Sorry, that password is invalid."<<endl;
    }

    }while(valid != 0);
    cout<<"The password is correct!"<<endl;

    return 0;
    }
  • BajanineBajanine Posts: 12Member
    : This is another simple password program I just wrote. The master password is a const char array so you can change it to whatever you like. When the password is correct, the program keeps going.
    :
    : #include
    : #include
    :
    : using namespace std;
    :
    : int main()
    : {
    : int valid;
    : const char str1[] = "password";
    : char str2[64];
    : do
    : {
    : cout<<"Enter the password: ";
    : cin>>str2;
    :
    : valid = strcmp(str1, str2);
    : if(valid != 0)
    : {
    : cout<<"Sorry, that password is invalid."<<endl;
    : }
    :
    : }while(valid != 0);
    : cout<<"The password is correct!"<<endl;
    :
    : return 0;
    : }
    :

«1
Sign In or Register to comment.