Thread: Enter password, hide by "*".

  1. #16
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Just for your info, there's an example of how to obtain a password from the user here.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  2. #17
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Sorry guys...looks like 24 bits to me. Maybe 3 bytes. Could be more.

    Quote Originally Posted by C++Child
    No its a 3 bit. 0 - 1 - 10 - 11. 11==3.
    This looks compiler-specific to me.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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. =(
    Actually, it's 3 bytes (prize of a ++rep goes to pianorain!).

    To use code tags, put [ code ] before your code and [ /code ] after your code (without the spaces, I'm just putting the spaces so that it doesn't think I'm actually using code tags ). Read the link in Salem's signature for more info.
    Just Google It. √

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

  4. #19
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, statistically speaking, it'll most likely be less than the equivalent of a 3-byte (24-bit) password. You are likely to have only 62 possible entries in those three spaces (a-z, A-Z, and 0-9), assuming you let the user type it in, instead of the maximum possible of 256 (2^8). So, instead of there being 256^3 (2^24) possible keys, there are 62^3. So, doing a bit of rounding so I don't need to pull out a calculator (62 ~ 64), we see that there are in fact ~64^3=2^18 possibilities, so what you have there is, in effect, an 18 bit (or 2.25 byte) password.

    Sorry, I couldn't help myself.

  5. #20
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, technically you CAN input direct ASCII values for the password. Hold ALT and press 1,2,3 and then release ALT, and you'll end up with the character corresponding to ASCII 123. I've done that for several of my DOS programs before I realized that you could get the same effect from outputting (char)123 Although, on Windows there seems to be some oddity where alt-023 would give a different result from alt-23... Of course, if the user sets his/her own password, it should be no problem anyways.

    Sorry, I couldn't help myself. ^2
    Just Google It. √

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

  6. #21
    I'm less than sure.... abyssphobia's Avatar
    Join Date
    Aug 2004
    Posts
    112
    ok now Im testing the code tags (thank you toysoldier
    Code:
    #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; 
    
    }
    I hope this works
    Have I crossed the line?

  7. #22
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Gee, how did you know my real name is toysoldier?

    **EDIT**
    And congrats on your first successful code tag
    Just Google It. √

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

  8. #23
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Read the link in Salem's signature for more info.
    I guess you mean mine?!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #24
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Sorry. I swear, it was a typo! *edits post in a lame attempt to cover up*

    **EDIT**
    Foiled. The edit button's gone on my post
    Just Google It. √

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

  10. #25
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Code:
    if(m[0]==n[0]&&m[1]==n[1]&&m[2]==n[2]) //stupid code!
    There's an easier way to compare strings in cstring. Use strcmp(). strcmp() returns a value of false (0) if both strings are the same. so say if(!strcmp(m[],n[])
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  11. #26
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>so say if(!strcmp(m[],n[])
    Code:
    if (strcmp(m, n) == 0)
    Or the equivalent but arguably less clear
    Code:
    if (!strcmp(m, n))
    However, in this case because the arrays are not terminated by a null character they are not technically strings of any kind. As such, strcmp will cause undefined behavior because it will test beyond the boundaries of the array.

    Even if the arrays were valid C-style strings you would still be wise to consider the overhead of a library function call over three simple equality tests. In some cases the difference may be enough to argue strongly in favor of the inline code.

  12. #27
    I'm less than sure.... abyssphobia's Avatar
    Join Date
    Aug 2004
    Posts
    112
    mmm ok thx everybody, also Hunter thx,and toysoldier, I owe u one, thx 4 the patience
    sorry If anyone get mad, Im sorry
    well practicing the the tips you gave,
    I just want to put how it looks 4 the newbies like me, Ive learned a lot!!!

    Code:
                    #include <iostream>
                    #include <cstdlib>    // original #include<stdlib.h>
                    #include <conio.h>  
      
                     using namespace std;                        
                    int main( )
                 
                   {
    
                          char m [4] = "toy";       
                          char n  [4] ="000";                         
                          cout<<" \n enter 3 bit password:"<<flush;
                       
                     n [0] = getch( );   // getch( ); 
                          cout<<" * "<<flush;   
                          
                     n[1]  = getch( );
                           cout<< "  * "<<flush;
                           
                      n[2]  = getch( );
                            cout<< "  * " <<flush;
                   
             if (strcmp( m , n ) = = 0 )  
                       {
                                      cout<<" password correcta, bienvenido"<<endl;
                       }
                  else
                   {
                                   cout<<"error "<<endl;
                   } 
              system ("pause");     
            return 0;
           }
    and
    Code:
    #include <iostream>
                    #include <cstdlib>   
    #include <conio.h>  
      
                     using namespace std;                       
                    
    int main( )
                 
                   {
    
                          char m [4] = "toy";      
                          char n  [4] ="000";      
                       
                          cout<<" \n enter password:"<<flush;
                       
                     n [0] = getch( );   
                          cout<<" * "<<flush;   
                          
                     n[1]  = getch( );
                           cout<< "  * "<<flush;
                           
                      n[2]  = getch( );
                            cout<< "  * " <<flush;
                   
             if (strcmp( m , n ) = = 0 )  
                       {
                                      cout<<" password correcta, bienvenido"<<endl;
                       }
                  else
                   {
                                   cout<<"error "<<endl;
                   } 
              system ("pause");     
            return 0;
           }
    ok guys tell me , what is the diference between the endl,flush like appears below

    Code:
    cout<<"whatever"<<endl;
    cout<<"whatever"<<flush;
    cout<<"whatever";
    thx in adv.
    love abyss
    Have I crossed the line?

  13. #28
    I'm less than sure.... abyssphobia's Avatar
    Join Date
    Aug 2004
    Posts
    112
    this is the princeton's way!!!
    Code:
    #include <iostream>
    #include <cstdlib>   
    #include <conio.h>  
      
                     using namespace std;                       
                    
    int main( )
                 
                   {
    
                          char m [4] = "toy";      
                          char n  [4] ="000";      
                       
                          cout<<" \n enter password:"<<flush;
                       
                     n [0] = getch( );   
                          cout<<" * "<<flush;   
                          
                     n[1]  = getch( );
                           cout<< "  * "<<flush;
                           
                      n[2]  = getch( );
                            cout<< "  * " <<flush;
                   
             if (!strcmp( m , n )  )  
                       {
                                      cout<<"this is the  password , welcome"<<endl;
                       }
                  else
                   {
                                   cout<<"error "<<endl;
                   } 
              system ("pause");     
            return 0;
           }
    Have I crossed the line?

  14. #29
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>what is the diference between the endl,flush
    endl will print a newline and flush the stream, flush will not print a newline and flush the stream.

    >>this is the princeton's way!!!
    Well, it is not the way I would do it, but that will suffice. I would do something more like this if asked to use conio.h.
    Code:
    #include <cstdlib>
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <conio.h>
    
    namespace {
      const std::string default_file("input.txt");
    }
    
    std::string decrypt(const std::string& pass);
    void get_pass(std::string& buffer);
    bool valid_pass(const std::string& buffer, const std::vector<std::string>& passwords);
    
    int main(int argc, char *argv[])
    {
      std::string buffer;
      std::vector<std::string> passwords;
      std::string pass_file(default_file.c_str());
    
      // Acquire filename and open file
      if (argc > 1) {
        pass_file.assign(argv[1]);
      }
      std::ifstream in(pass_file.c_str());
      if (!in.is_open()) {
        std::cerr << "Error opening password file" << std::endl;
        return EXIT_FAILURE;
      }
    
      // Load passwords into memory
      while (std::getline(in, buffer)) {
        passwords.push_back(decrypt(buffer));
      }
      buffer.clear();
    
      // Get user password and validate
      get_pass(buffer);
      if (valid_pass(buffer, passwords)) {
        std::cout << "Valid password" << std::endl;
      }
      else {
        std::cerr << "Invalid password" << std::endl;
      }
    
      return EXIT_SUCCESS;
    }
    
    std::string decrypt(const std::string& pass)
    {
      // Placeholder for a password encryption scheme
      return pass;
    }
    
    void get_pass(std::string& buffer)
    {
      int ch;
    
      while ((ch = getch()) != '\r') {
        if (ch == '\b') {
          // Handle backspacing intelligently
          buffer.erase(buffer.end() - 1);
          cprintf("\b \b");
        }
        else {
          buffer.push_back(static_cast<char>(ch));
          putch('*');
        }
      }
      putch('\n');
    }
    
    bool valid_pass(const std::string& buffer, const std::vector<std::string>& passwords)
    {
      std::vector<std::string>::const_iterator it = passwords.begin();
      const std::vector<std::string>::const_iterator end = passwords.end();
    
      for (; it != end; ++it) {
        if (buffer == *it) {
          return true;
        }
      }
    
      return false;
    }

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