This lab should provide you with practice in file handling and string manipulation. Classes are pretty much useless here. *grin*
Write a program that allows the user to search through a list of names stored in a file for a particular one. (Make sure to allow the user to tell you the name of their file and to check for file open errors.) The names in the file will be stored one per line (spaces are allowed in the names). You won't know the length of the list of names ahead of time and it could be a ridiculous number of names — hundreds of thousands or even millions!
Although this might seem like the perfect place to use strcmp, most people think of searches as case insensitive — and strcmp is case sensitive. You'll have to write your own version of strcmp which is case insensitive to use in the program. (Maybe strcmp_ncase?)
You can choose the actual names to use for your data. Make sure to do enough sets of data to well test your program! (Empty data set, find first item, find last item, find a few in the middle, etc.)
Remember that your program cannot know how many data names are in the file ahead of time! In this context, this means that you cannot store all the names in your program's memory — not even using dynamic memory or a vector.
Don't forget to read the file's name from the user and protect your program against any errors that may occur during the opening of the file.
Use functions to break up the program into more manageable pieces.
As an example, you might have the data file contain:
Jason James Mary Jones Tammy Henry Bob Smith
and the program interaction might look something like (the parts in this color are typed by the user):
$ namesearch.out Welcome to the Name Searching Program!!! Please enter the name of your names file: bob.dat I'm sorry, I could not open 'bob.dat'. Please enter another name: names File 'names' opened successfully! What name would you like to find in this file? tammy henry 'Tammy Henry' is the 3rd name in the file! Thank you for using the NSP!! Endeavor to have an amazing day! $
Don't forget about the occasion when the name they want isn't in the file at all!
If you want it to look nice (with the rd on 3rd instead of just 'position 3'), please see the option below.
How do you handle not knowing how much data is in the file?
What kind of loop do you use to process the file?
What do you do if the person isn't found in the file?
Can there be more than one name on a single file line? (Would your program handle it? Don't change it to make it — just answer the question.)
This assignment is (Level 1.5).
Add (Level 1.5) to place a loop in your main to allow the user to search the same file multiple times before quitting. (Warning: You'll be searching through the same file several times — always start a search from the beginning of the file!)
Add (Level 1) to match not on the whole string but on any substring match. (This is similar to the lab from before. Of course, now the baby option of an optional starting location is required and worth no higher level. Also you'll have to retain the case-insensitive requirement for THIS lab. The two advanced options are available here, too, but are each reduced by 1: level 1 and level 2 respectively.)
Add (Level 0.5) to print all matches in the file instead of just the first one found.
Add (Level 0.5) to make the name's position print nicely (1st instead of 'position 1', etc.). For more on making such functionality, see the lab originally given in 121 (you'll have to answer all its "Thought Provoking Questions" for full credit; its options are available, but each is worth only Level 0.5).