Topical Information

This lab should provide you with practice in file handling and string manipulation. Classes are pretty much useless here. *grin*

Program Information

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.

Thought Provoking Questions

(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!)

  1. How do you handle not knowing how much data is in the file?

  2. What kind of loop do you use to process the file?

  3. What do you do if the person isn't found in the file?

  4. 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.)

Handing it in...

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

Options

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!)


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)