CS 113: Introduction to Computer Science II with Introduction to C

Steve Homer---Spring 1999

Homework 3---due Thursday, March 4 before 10:00 PM

Reading: Kelley and Pohl, Chapters 9, 11 and 12, pages 327-338 and 385-435.


The files you must submit for this homework are exactly: h3main.c, h3invent.h, h3invent.c, and h3.scr.

You are to write a program that maintains inventory for a store. The program will essentially use an array to hold these items.

The store has many different products. For each product, you will keep track of its:

You will also write operations to add/remove items and get/change prices.


You must separate the code that implements inventory into an inventory module (see Lab 4 about modules) as follows.

Inventory Interface File

The interface for the module, which we have provided as the header file h3invent.h, contains the constants and types needed for inventory. In addition, it contains prototypes for all the functions users of the inventory module (i.e., the code in the main program) will call.

Make sure you document this header and wrap it (see Lab 4 for wrapping headers).

Inventory Implementation File

The inventory module's implementation file should contain function definitions for the 6 functions prototyped in the header file. We have provided a skeleton for this implementation file, h3invent.c, for you. You may add other helper functions to this .c file if they will help you implement those 6. Any extra helper functions should be prototyped in this .c file (i.e., not in the .h file). Also, these helper functions should not be called directly by people using the module, i.e., in the main program.

Below is how each inventory function should operate and how you might call each in the main program (the list of inventory is always passed by reference so that it can be changed by functions that must do so).


Note that these inventory functions do not print out error messages. Instead, they return constants (like INVENT_NO_ROOM) on errors. Since these constants are all negative numbers, they will never be confused with counts or prices (for functions that also return counts or prices upon success).

Thus, it is the main program's responsibility (described below) to examine these return values and print out proper error messages.


Main Program

We have provided a skeleton for the main program, which must go in a file named h3main.c. It should work as follows:

  1. The program will start by reading in inventory from the file: /cs/course/cs113/current/hw3/inventory.dat
  2. It should then output a list of operations that the user can choose. Operations include: adding products, selling products, looking up a product's price, and changing the price of a product. Thus, some operations will change the inventory.
  3. The user should choose an operation and the program will prompt the user for whatever information is needed to carry out that operation, which then will be performed.
  4. The program should prompt for more operations until ``q'' is entered.

Error Checking

Remember that your program should print descriptive error messages when the inventory functions return error conditions. Also, your program should check that quantities and prices entered by the user are always > 0.

Example Run

Here is an example run (things the user types are in bold):
% h3inventory
Initial inventory read from:
/cs/course/cs113/current/hw3/inventory.dat

Please choose one of the following operations by typing its
letter and then pressing <Return>:

  a) Add some items.
  r) Remove (sell) some items.
  p) Show the price for an item.
  c) Change the price of an item.
  q) Quit inventory.

Choice> a

Item name: Pinesol
Add quantity: 5
Unit price (in cents): 199

There are now 12 units of "Pinesol".

Choice> r

Item name: Pinesol
Sell quantity: 2

There are now 10 units of "Pinesol".

Choice> p

Item name: Pinesol

A unit of "Pinesol" goes for $1.99.

Choice> c

Item name: Pinesol
New unit price (in cents): 299

Price of "Pinesol" changed by $1.00.

Choice> p

Item name: Turtlewax

No such item "Turtlewax" in inventory!

Choice> a

Item name: Pinesol
Add quantity: 10
Unit price (in cents): 299

There are now 20 units of "Pinesol".

Choice> q
Bye! Thanks for using inventory.


A Make File

We have written a make file for this program that will generate the executable ``h3inventory'' for the inventory program above.

This make file is named h3makefile. Since the utility make normally expects the makefile to be named ``Makefile'' or ``makefile'', run the make utility with:

make -f h3makefile


Script and Test Runs

Your script file (h3.scr) must contain: each source code file (use "cat" to display h3main.c, then h3invent.h, then h3invent.c), compilation (using our make file), and test runs (in that order). You may construct your own test runs, but they must demonstrate each of the operations, plus all the error handling. Your tests will have to be more elaborate than the example run above, since ours does not test all the error conditions; however, don't make your test runs longer than is necessary to demonstrate all the features.