CSCI 160 Lab exercise 5, F21N01/N02

This lab involves the use of several new language features: command line arguments, file input, and (arrays of) structs. You have three weeks to complete the lab, and I strongly recommend you start early!

In lab exercise 5 we'll be working on using command line arguments, reading data from files, and storing/accessing data in arrays of structs.

As with previous labs, there are several key syntax elements you'll need to be familiar with. The core syntax elements can be found here.

The process for obtaining, working on, and submitting labex5 is the same as for past labs (except of course it's labex5 rather than labex0/1/2/3/4).

Refer back to the lab1 part 1 instructions if you forget the specifics of different steps.

labex5.cpp objectives

In this lab exercise we'll be utilizing a great many of the features we've worked with previously, in addition to our work with structs and file/io. Libraries you may find useful include iostream, sstream, fstream, string, cstring, cctype, cmath, cstdlib, and cstio.

The applied objective for this exercise is to create and submit a complete and correct C++ program, adhering the the course code standards, with the following behaviour and restrictions:

  1. The user is going to provide the name of a file as a command line argument when they execute the program, e.g.
    ./labex5 somefilename
    Your program will thus be using main's argc and argv parameters to check for an access the provided filename.
    (To be clear: your program should NEVER be prompting the user to enter the filename.)

  2. The file itself will contain zero or more lines of text, each of which will have three entries: a nickname (single word), a username (single word), and an id (positive integer). For example:
             shaggy rogersn 9139
             scooby doos 20410
             velma dinkleyv 635
             freddy jonesf 0917
             daphne blaked 7403
        

  3. Your program will open the file and read the contents, storing them in an array of structs (max array size 20), using the following struct definition:
    struct UserInfo {
       string nickname;
       string username;
       int userid;
    };
    

  4. The user can then repeatedly enter values, each of which could be a username, nickname, or user id. After each value entered, the program will search the array and either print the three fields associated with that value or inform the user that no matching entry was found.
    The user indicates they want to end the program by entering QUIT as the search value.

  5. Don't forget to check that the file opened successfully and to close the file when finished with it.
    If the user does not provide a command line argument, or your program is unable to open the file for reading then is should print an appropriate error message and end the program.

Sample run

The table below shows a sample run of the program. Note that you do not have to use the exact same prompts or error messages as those shown below, but be sure your prompts and messages are clear and informative.

This shows the original linux prompt as a $ and the command the user entered to start the program running (./labex5 mydata), then shows the resulting run of the program.

This particular example assumes the content of the mydata file happens to be
shaggy rogersn 9139
scooby doos 20410
velma dinkleyv 635
freddy jonesf 0917
daphne blaked 7403

Just to make this display a little clearer, I've highlighted what the user entered in bold italics. (That's not something you could/would do in your program.)

$ ./labex5 mydata

Welcome to the userlookup program!
The user database has been initialized from file: mydata

Please enter your list of search values, ending with QUIT when done.
635
velma dinkleyv 635

doos
scooby doos 20410

7403
daphne blaked 7403

1234
No matching data was found for 1234

daphne
daphne blaked 7403

abc
No matching data was found for abc

QUIT
Bye!

Deadlines and late penalties

Submission deadlines and late penalties for all lab exercises are provided on the main labs page.

Reminders

Don't forget to do your "make submit" when finished, and don't forget about the tools/tips from week 2 of labex1 (ssh'ing to the pups, getting your lab feedback, and use of X2Go).

Don't forget to follow the code standards, be sure to check your feedback from previous labs to see if there were concerns with your previous work with respect to the code standards.

Suggested (iterative) development process

As with previous labs, I strongly recommend starting early and following an incremental development process.