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"
...

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:

"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

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:

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

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.

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!

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?

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.

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.

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.