password search list

This is a discussion on password search list within the C++ Programming forums, part of the General Programming Boards category; Hiya, Say, I've got 500 passwords and usernames, for example: "bss01, Boss of the year" "mngr01, Manager of the month" ...

  1. #1
    Registered User 4WheelDrive's Avatar
    Join Date
    Mar 2004
    Posts
    5

    Question password search list

    Hiya,

    Say, I've got 500 passwords and usernames, for example:

    "bss01, Boss of the year"
    "mngr01, Manager of the month"

    . . . . . . .

    Q1:

    Can I declare just one array for it?
    Or do I have to have two pointers of array, like:-

    Code:
    char *passlist[] = {"bbbs01", "michelle01", "gwen07", "laura01"};
    	char *namelist[] = {"Baba BlackSheep", "Michelle Mystique", "Gwendoline Gorgio", "Laura Lavidd"};
    So when the search password is found it displays the name that belongs to the password, for example if enter laura01 in the program it would display:-

    Welcome: Laura Lavidd

    Q2:

    If I change the array into text file and then read it in for the program to search. How do I let the program know of how many names, or how big is the file?

    Code:
    	//search for password
    	for (int p = 0; p <= 3; p++)
    		if (strcmpi(passlist[p], pword) == 0)
    		{
    			cout << "\n\nWelcome: " << namelist[p] << endl;
    			break;
    		}
    		if (strcmpi(passlist[p], pword) != 0)
    			cout << "\nPassword not found !!" << endl;
    Here is my demo prog in BC ++, it works pretty well as is for now.

    And thanks for any comments and advice you can give.

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>
    #include <string.h>
    
    int main()
    {
    	char *passlist[] = {"bbbs01", "michelle01", "gwen07", "laura01"};
    	char *namelist[] = {"Baba BlackSheep", "Michelle Mystique", "Gwendoline Gorgio", "Laura Lavidd"};
    
    	int ch;
    	char pword[BUFSIZ];
    	int i = 0;
    
    	cout << "Enter your password: ";
    
    	while ((ch = getch()) != EOF
    			&& ch != '\n'
    			&& ch != '\r'
    			&& i < sizeof(pword) - 1)
    	{
    		if (ch == '\b' && i > 0)
    		{
    			putchar('\b ');
    			i--;
    			pword[i] = ch;
    		}
    		if (isalnum(ch))
    		{
    			putchar('*');
    			pword[i] = ch;
    			i++;
    			if (i >= 10) break;
    		}
    	}
    	pword[i] = '\0';
    
    	//search for password
    	for (int p = 0; p <= 3; p++)
    		if (strcmpi(passlist[p], pword) == 0)
    		{
    			cout << "\n\nWelcome: " << namelist[p] << endl;
    			break;
    		}
    		if (strcmpi(passlist[p], pword) != 0)
    			cout << "\nPassword not found !!" << endl;
    
    	return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Use a map<string,string> container to store the username/password pairs (or password/username pairs if you plan on needing to search the container based on passwords). You can insert elements into the container and it will grow to however big it needs to be. If your continer holds password/username pairs searching for a password is easy:

    Code:
    #include <map>
    #include <iostream>
    #include <string>
    using namespace std;
    ...
    map<string,string> PassUserMap;
    // Insert some password/username pairs into the container
    PassUserMap["bbbs01"] = "Baba BlackSheep";
    PassUserMap["michelle01"] = "Michelle Mystique";
    PassUserMap["gwen07"] = "Gwendoline Gorgio";
    PassUserMap["laura01"] = "Laura Lavidd";
    //Search for "laura01" password.
    if( PassUserMap.find("laura01") != PassUserMap.end() )
        cout << "\n\nWelcome: " << PassUserMap["laura01"] << endl;
    else cout << "\nPassword not found !!" << endl;
    Displays:
    Code:
    
    Welcome: Laura Lavidd
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User 4WheelDrive's Avatar
    Join Date
    Mar 2004
    Posts
    5
    hk_mp5kpdw:

    Thanks for your reply. For the ease of up date I'd like to store the password list in a text file, can I sttill do that. And how do I search the whole user map? same in a loop?

    Thanks again, I will now go off and play around with your suggestion.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    You mean store just the passwords alone or the entire password/username data in a text file? Either way, yes it is possible. Assume you store passwords and usernames in the textfile such as:
    Code:
    bbbs01 Baba BlackSheep
    michelle01 Michelle Mystique
    gwen07 Gwendoline Gorgio
    laura01 Laura Lavidd
    Then you simply need to open the file and loop through it getting the data and inserting it into the map. Since the username may consist of more than one word separated by whitespace ' ' characters, that will make parsing this information a tiny bit more complicated. Basically you would need to read in the first string, i.e. the password, and then use the getline function to read in the remainder of the data (first and second names together) into a second string. You will now have two strings containing the password and username for a single user. You can insert these values into the map as I have shown you but instead of hardcoded values you would use the variable names of the strings that are holding your password and username. Put all of that code into a loop that will read data until the end of the file and you will be able to insert all of your password/username pairs into the map.

    If you wanted to go through the whole container and output all the password/username pairs to the screen for example, you would need to use an iterator and a loop. An iterator is basically a pointer to the data, in this case a pair, and is used as a loop control variable. Since we are dealing with a pair, the iterator has two members, first and second where first holds the key from the map container and second holds the value. As an example:
    Code:
    map<string,string>::iterator it;
    for( it = PassUserMap.begin(); it != PassUserMap.end(); ++it )
      cout << "Password: " << it->first << " Username: " << it->second << endl;
    Using the passwords/username given previously this should output something like:
    Code:
    Password: bbbs01 Username: Baba BlackSheep
    Password: gwen07 Username: Gwendoline Gorgio
    Password: laura01 Username: Laura Lavidd
    Password: michelle01 Username: Michelle Mystique
    Notice that a map container will automatically sort its data according to the key member so that we get an alphabetized order of the passwords. You could use a similar loop to search through and determine if a given username existed in the container by looking at the second member of the iterator and comparing it to whatever you are trying to find.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User deadpoet's Avatar
    Join Date
    Jan 2004
    Posts
    50
    I would follow hk_mp5kpdw advice and use a map container. It provides a very easy to use indexed key-value container to store data.

    DeadPoet

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    While using a map may be an efficient way to do this, it's not the only way. As an alternative, you could use a user defined struct/class to hold each pair passward/information and an array or list to hold a group of objects representing the information from the file in your program. Many people don't learn about STL containers such as maps right away.

    If you know about maps, are comfortable with other STL containers and are willing to learn about maps, or are interested in learning about STL containers from scratch, go for it. But there are other ways to do this, as there is in most programs.

  7. #7
    Registered User 4WheelDrive's Avatar
    Join Date
    Mar 2004
    Posts
    5

    Question

    Ah yeh... looks very neat and it's surely what I'm looking for, a method of storing password and username pairs, but unfortunately, it's a bit complicated for someone new like me. Have not learned class yet let alone container.

    Also my compiler does not seem to support <map> <string>, though it's got <string.h>
    but I think it works differently from <cstring>

    I need a new compiler!
    Any suggestion as to which is the best and up to date? Will the free DevC++ compiler help me to use your example?

    Or I will have to put this program on hold while trying to up grade a compiler!

    Many thanks for replying so quickly.

  8. #8
    Registered User deadpoet's Avatar
    Join Date
    Jan 2004
    Posts
    50
    Oh yes, you are correct Elad this is not the only way. Structures are a very solid part of any simple and/or complex data structure. You can even stick a pointer to a structure into a map comtainer and just about anything else that you can think of.
    I personally like looking at data from the view point of hash of hashes, hash of arrays, and even better hash of hashes of array. I think I am showing my roots from Perl programming but it applies in C++ programming and there is nothing better than a solid data structure. I may not be a full-blown OOP developer and that I where I will yield to those who do it for a living, but the points that must be made and determined is:
    ** what is it the developer want to accomplish?
    ** what is their current level of understanding?
    ** do they wish to push thier skill just a little bit to learn?

    DeadPoet

  9. #9
    Registered User 4WheelDrive's Avatar
    Join Date
    Mar 2004
    Posts
    5
    elad

    I just missed your post...the only way I could make the program work based on what I had learned so far was to used two arrays, one to hold password list and one to hold username list, as already used in my program.

    Guess, I will have to read up on struct/class fast, eh?

    Thanks

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If you don't know about structs/classes or STL containers yet, then the dual arrays where indexes are used to "connect" the password with the information is the technique to use. The other approaches will have a lot to offer in this type of a problem when you learn about them. If you want to use this program as a means to explore these other techniques, use a book or a tutorial to get the basics and then come here when you have questions/problems. For now, you can keep in mind that there are other, "better??" ways to do this.

    Unfortunately, arrays using static memory (the type you learn about first) have a limited capacity. If you have 500 passwords and 500 associated information strings you may exceed that capacity and introduce some unexpected and undesired behaviors in your program. In that case, you will either need to decrease the number of passwords read into the program at any given time (that is, read and write to the file on an ongoing basis, rather than a single read/write) or use/learn about dynamic memory, which generally has a significantly increased capacity than static memory.
    Last edited by elad; 03-12-2004 at 08:20 AM.

  11. #11
    Registered User deadpoet's Avatar
    Join Date
    Jan 2004
    Posts
    50
    What compiler are you using and for what operating system?

    DeadPoet

  12. #12
    Registered User 4WheelDrive's Avatar
    Join Date
    Mar 2004
    Posts
    5
    OK, elad ... I will keep your suggestion and advice in mind, thanks.

    deadpoet:

    I am using borland C++ on windows xp home, I also use (on removeable drive) linux mandrake 8.0 which comes with gnu c++

    Do you think I need a more up to date compiler?

    Cheers.

  13. #13
    Registered User deadpoet's Avatar
    Join Date
    Jan 2004
    Posts
    50
    I am suprised that Borland C++ compilers does not have map. Is this like a freebee version that came with a CD? If you are using Linux than you can get the gcc compiler from FreashMeat . The offical gcc website is http://gcc.gnu.org and contains a port for Microsoft Windows. I think that you would be better served, for the long haul, in learning some of the STL containers like map and vector. However, I full support Elad's response for learning structures. You will not get around structures in C/C++ programming they are everywhere; and you need to know how they work, when to use them and where to use them.

    DeadPoet

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    The freebie Borland compiler comes with standard C and standard C++ libraries, including everything you need for the STL map class.

    (My version is 5.5.1; don't know about earlier versions.)

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 07:02 PM
  2. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 06:29 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21