for codeYourLife only (week 5)
SAMPLE PROGRAM #3: PARALLEL
ARRAYS (USERNAME AND PASSWORD)
In this section we examine a problem that involves maintaining two parallel arrays. The first array holds usernames and the second array holds passwords. The two arrays are separate but related, each having the same number of elements, and the values in one correspond to the values in the other. For example, the seventh element in the username array and the seventh element in the password array together form a valid username/password combination that would grant authentication to the system.
Write a program that reads 10 username and password pairs and stores those username and password values into parallel arrays. After the arrays have been loaded, the program should behave as a login screen, prompting for a username and a password. Based on the data entered for username and password, the program should respond appropriately with one of three output messages. If the username does not match one of the values in the username array, then the message should be “Username not found.” If the username is found in the username array, but the password does not match the parallel value in the password array, then the message should be “Username and password do not match.” If the username is found and the password matches the parallel value in the password array, the message should be “Access granted.”
READING DATA FROM A TEXT FILE
In addition to Dialog and Console input, there is a third input option called File input, which allows the input data to come from a text file rather than being manually typed in each time the program is executed. In this program, for example, the 10 username and password pairs
can be entered one time into a simple text file and those values can then be read from the text file into the parallel arrays each time the program is executed. This will eliminate redundant typing and reduce the likelihood of a typing error.
To perform file I/O, double-click on the Input (or Output) element to show the edit dialog, click “More ”, select the File option button, and then specify the appropriate text file name.
Four tips to remember when doing file I/O:
1. First, when creating our input text file, DO use quotes around string data inside the text file. (Note that File input works the same as Console input in that strings must have delimiting quotes, and that the delimiters can be either single or double quotes, just so long as you use the same delimiter to start and stop the string.)
2. Second, do NOT use quotes around the text file name in the I/O dialog.
3. Third, if your file name in the I/O dialog box uses a relative reference (without the full path name), the text file needs to be in the same folder as the Visual Logic.exe executable (which is not necessarily the same folder as the *.vls solution file).
4. Fourth, if you have both File Input and File Output in the same program, you must use different file names for the Input and Output files.
ASK THE AUTHOR
Q: Are usernames and passwords typically saved as text files?
A: No. (Thank goodness.) A text file with usernames and passwords would not be very secure and you certainly would not want your bank account information stored in this format. An enterprise system would most likely store username and password data in a password-protected database.
ANALYSIS AND DESIGN
We begin our implementation of the username and password solution by confirming that we have a valid text file with username and password pairs. Figure 5-11 shows the username and password pairs from the username.txt file. Note that the first four username and password pairs are “peanut butter”/”jelly,” “sunrise”/”sunset,” “light”/”dark,” and “forward”/”reverse.” Those values are read and stored into parallel arrays that will look like Figure 5-12.
Figure 5-11 Usernames.txt input file
Figure 5-12 Parallel arrays after reading data from input file
Once the parallel arrays have their data, the program prompts the user for a username. The program checks the user’s input against the Username array, looking for a match. If the entire array is searched and no match is found, then the program gives the “Username not found” error message. If the username is found, the program prompts the user to input the associated password. The program then looks in the Password array at the same index value location where the Username was found. If the user-entered password matches the associated password in the parallel array, then the program gives the “Access granted” message; otherwise the program gives the “Username and password do not match” error message. The solution to this problem is shown in Figure 5-13,