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.
(As always, these are meant to provoke your thoughts and push you in the direction of a good solution to the programming problem. You should answer these now before you start coding your own solution. On occasion, you may not understand what a question means before coding, but you'll find it helpful during the coding process more than likely.
Also note that since these questions are meant to
'provoke thoughts', they all imply a '
why', '
why not',
'how', or the like as follow-up.
Never is a simple 'yes' or
'4' the answer — no matter how
much you'd like it to be. Always answer in complete
sentences and explain your answer/choice!
And remember: you won't get full credit unless you answer these questions — appropriately!)
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.)
After you've placed your TPQ answers in a separate file named like your main program file, you'll need to run your script. Don't forget to execute your program as many times as you need to show a good sample of testing your program! Finally, if you want/need corrections, go to your favorite email and send me the PDF with a proper subject.
This assignment is (Level 1.5).
You may choose to alter your program from above to add any of the following (bulleted) options. You won't be handing in a separate program (unless specifically told to do so!). You will simply change the program you already have to adhere to the new specifications below. This change is considered significant enough to increase your level as stated below.
Again: do NOT create/hand-in multiple programs! Simply altering the program you have already written is enough to increase its level by the amount specified in the option you choose...
(Note: If the option includes its own TPQs, you must answer them in addition to the ones above to earn the full credit!)
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).
Last modified 12/26/2022 23:15:22.
It is now 3/15/2025, 7:07:22 AM.
Date you last viewed this page: That's for you to know and ...well, that's for you to know.
© 1993-2025 Jason James (email — craie@acm.org — for permissions &/or details)