Thread: Enter password, hide by "*".

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    98

    Enter password, hide by "*".

    I write a little program, just want to test getch() function.
    When you enter password, they dont display in monitor,
    instead of "*". It 's running OK.
    But some statements look very stupid,
    please give some improvement!
    thanks.

    PHP Code:
    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>

    void main()
    {
        
    char m[3]={'t','o','y'};   //to save original password.
        
    char n[3]={'0','0','0'};  //initialize your entering password.
        
    cout<<"Please enter 3 bit password: "<<flush;  
        
    n[0]=getch();            //enter word ,but no display it              
        
    cout<<"*"<<flush;    //when enter word, display “*”
        
    n[1]=getch();
        
    cout<<"*"<<flush;
        
    n[2]=getch();
        
    cout<<"*"<<flush;
        if(
    m[0]==n[0]&&m[1]==n[1]&&m[2]==n[2])   //stupid code!
        
    {
            
    cout<<"password correct,welcome!"<<endl
        
         }
        else
        {
            
    cout<<"error."<<endl;
        }

    system("pause");    //pause running window.
    return;


    Last edited by toysoldier; 08-12-2004 at 05:28 AM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    it looks ok to me besides the
    Code:
    void main()
    main should always return a int
    Code:
    int main(void)
    {
      //code
      return 0;
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    @ prog-bman :
    OH, it's a tip "int instead of void" or rule ?

    How about this statement:
    if(m[0]==n[0]&&m[1]==n[1]&&m[2]==n[2]) //stupid code!
    Thanks.
    Last edited by toysoldier; 08-12-2004 at 05:42 AM.

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    That looks fine.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>OH, it's a tip "int instead of void" or rule ?
    It is a rule. The C++ standard requires main to return an integer except in very specialized implementations where it imposes no restrictions. The general rule of thumb is that if you do not know whether or not you are using such an implementation, you are not and thus the relaxed rules do not apply to you. As such, instead of saying that every time somebody uses void as the return type for main, most helpful souls will simply tell you that returning void from main is illegal or wrong or evil or something similar for the sake of brevity.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    @ Princeton :
    Thanks,
    I think I have learning a lot of valuable knowledge after entered this great forum.
    Thanks every friend !

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You will probably learn about control loops in the near future. They will provide you with the techniques to "clean up" the sections of the code that you felt uncomfortable with.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    One other thing.
    <iostream.h> is no longer standard, use <iostream> and the std namespace (putting 'using namespace std;' after your includes is the easiest to start with).
    <stdlib.h> is standard C, but it is good to get in the habbit of using <cstdlib> in C++ along with the namespace.
    Just be aware that <conio.h> is a non-standard header and you may or may not find it if you change compilers.

    Code:
    #include <iostream>
    #include <conio.h>
    #include <cstdlib> 
    
    using namespace std;

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>if(m[0]==n[0]&&m[1]==n[1]&&m[2]==n[2]) //stupid code!
    An easier way to do this would be:
    Code:
    char m[4]="toy";   //4th character is '\0' (null). 
    char n[4]="000";  //same as above.
    
    (...)
    
    if(strcmp(m, n) == 0)   //compare the strings m and n
    {...}
    else
    {...}
    
    **OR**
    
    char m[3] = {'t','o','y'};
    char n[3] = {'0','0','0'};
    
    (...)
    
    if(memcmp(m, n, 3) == 0)   //compare 3 elements of m and n
    {...}
    else
    {...}
    Hope this helps!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    I'm less than sure.... abyssphobia's Avatar
    Join Date
    Aug 2004
    Posts
    112
    ok your code corrected will be like this:

    #include <iostream>
    #include <cstdlib>
    #include<conio.h>

    using namespace std;

    int main()
    {

    char m[3]={'t','o','y'}; //to save original password.

    char n[3]={'0','0','0'}; //initialize your entering password.
    cout<<"\n"
    cout<<"\nPlease enter 3 bit password: "<<flush;

    n[0]=getch(); //enter word ,but no display it

    cout<<"*"<<flush; //when enter word, display “*”

    n[1]=getch();

    cout<<"*"<<flush;
    n[2]=getch();
    cout<<"*"<<flush;
    if(m[0]==n[0]&&m[1]==n[1]&&m[2]==n[2]) //stupid code!
    {
    cout<<"password correct,welcome!"<<endl;

    }
    else
    {
    cout<<"error."<<endl;
    }

    system("pause"); //pause running window.
    return 0;

    }


    but I ran it,mmm the pasword is toy!!!!

  11. #11
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    just to let you know that isn't a 3 bit password

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I like the original code better. Slightly less standard, but it's code-tagged (Actually PHP tagged, but that's better than nothing), and the spacing is more intuitive. Abyssphobia, use code tags!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    I'm less than sure.... abyssphobia's Avatar
    Join Date
    Aug 2004
    Posts
    112
    yeah it's 4 bit pasword!!! zero is null duh!!mmm sorry a little distraction
    mmm code tags?
    How?? I read the post but I didnt got it. =(
    you know, Im gonna print the post to look it better=)

    regards,

    Abyss

  14. #14
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    No its a 3 bit. 0 - 1 - 10 - 11. 11==3.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  15. #15
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    what, no.
    Code:
    #include <limits.h>
    number_of_bits=CHAR_BIT*(strlen(m));
    number_of_bits+=CHAR_BIT;
    plust your code doesn't have enough room for a terminating '\0'(if I enter three characters)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. Replies: 2
    Last Post: 01-07-2009, 10:35 AM
  3. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 03:42 AM
  4. hi need help with credit limit program
    By vaio256 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2003, 12:23 AM
  5. terminate 0 - PLEASE HELP
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 11-21-2001, 07:30 AM