CMSC 106 Project #7 Spring 2001

Due date: Monday, May 14

1. Purpose

In this project you will write a program using structures and structures which contain other structures as members, arrays of structures, and functions with structure parameters, using both call-by-value and call-by-reference.

For this project you will be managing a bakery. You will have a list of ingredients you have in stock and a list of recipes you make with those ingredients. When an order comes in, you must determine if you have enough of all the ingredients required in order to fill that order. If you do not have enough of all of the ingredients, you must tell which ones have insufficient quantity.

2. Project description

As you read this section, you may also want to refer to the ``Sample output'' section below.

2.1 Program input

There will be three input files.

  1. The first input file will be a list of ingredients you have in stock. Each line will contain the name of that ingredient followed by the quantity you have on hand of that ingredient. The name of the ingredient will be a single word (no spaces) but can contain uppercase, lowercase and punctuation characters. The quantity will be a positive real number followed by a single character abreviation for the units. For the units, the c or C will be used to indicate cups, t will be used to indicate teaspoons, T will be used to indicate tablespoons and Q or q will be used to indicate quarts. The last line of this file will have the word ``done'' alone on the line. An example of one line of this file would be:

    flour   18.5 c

    This would mean you have 18 and a half cups of flour on hand. The items on this line will be separated by white space (one or more blanks) and there could be blanks at the beginning or end of the line. Any alternative spelling of the name of an ingredient should be considered a completely different ingredient, but the name with a different pattern of capitalization should be considered to be the same item.

  2. The second input file will be a list of recipes you cook at your bakery. Each recipe will have a name which it on a line by itself. This name should again be considered not case sensitive. The lines following the name will contain the list of ingredients needed for that recipe in the same format as the list of stock ingredients contained in the first input file. The list of ingredients for this one recipe will end by having the word ``done'' alone on the line after the last ingredient. The last line of this input file will contain the word ``lastrecipe'' on a line by itself to indicate that there are no more recipes. One recipe in the file could look like the following:

    flour 3 c
    yeast 2 t
    milk 1.2 c
    sugar 2 T

    This would mean that in order to make 1 order of bread you need to have 3 cups of flour, 1.2 cups of milk and 2 tablespoons of sugar, and 2 teaspoons of yeast. The items on each line will be separated by whitespace (one or more blanks) and there could be spaces before or after the line.

  3. The third input file will be a customer order being placed with bakery. It should contain a list of recipes (hopefully) from the second input file which will then be made using the ingredients from the first input file. Your goal here will be to determine if you have the recipes for the items requested and if you have enough of each of the ingredients to fill the order. The first line of this file will contain the name of the customer. The name can contain spaces, but won't be longer than 80 characters. The format of the rest of this file will be a quantity followed by a name of a recipe. The quantity will be a positive integer telling how many of that recipe the customer would like you to prepare. The list of items requested will end with the end of file marker. You do not need to worry about the possibility of the same recipe (item) being requested more than one time in this list, you may assme your customer is too organized to allow that to happen.

2.2 Processing and output

  1. The first input file will be a list of ingredients you have in stock. Before starting to process the first input file, you need to print a line that says the exact phrase PROCESSING STOCK INGREDIENTS to the screen. You will need to read each ingredient and store it into your (stock) list of ingredients if it is valid according to the definitions that follow. If the name is written into the file using any uppercase characters, you should convert it so that it is all lower case characters to make later comparisons easier. When you are finished processing this first input file (even if nothing was printed to the screen during the processing, you should print one line containing the exact phrase DONE PROCESSING STOCK INGREDIENTS.

  2. The second input file will be a list of recipes you cook at your bakery. Before starting to process this second input file, you need to print a line that says the exact phrase PROCESSING RECIPE LIST to the screen. The name of the recipe needs to be read from the input file. The list of ingredients should be read in and put in alphabetical order like the first input file's list. All of the same rules and error messages should be used when reading this list of ingredients as were used when reading the stock ingredients. The items on each line will be separated by whitespace (one or more blanks) and there could be spaces before or after the line. If the name of the recipe or the name of the ingredient has any uppercase characters, convert them to their lowercase equivalent to make later comparisons easier. After you read the recipe, you need to be sure it is a valid recipe according to the rules below before storing it in your (cookbook) list of recipes. The list of recipes should just remain in the order read from the input file. They do not need to be alphabetized. When you are done processing the information of the second input file, you must print one line containing the exact phrase DONE PROCESSING RECIPE LIST even if nothing was printed to the screen during the processing of this input file.

  3. The third list will be a customer order being placed with the bakery. Before starting to process this third input file you must print a line with the exact phrase PROCESSING CUSTOMER ORDER. You must first read and store the customer's name to be printed in the final communication to that customer.

2.3 Program limits and assumptions

3. Project requirements

All your C programs in this course should be written in ANSI C, which means they must compile and run correctly with cc -std1 -trapuv on the OIT UNIX Class Cluster. You will lose credit if your program generates any warning messages when it is compiled. Prototypes must appear for all functions used, listed at the top of the program file.

You may use any ANSI C language features we have discussed in the course so far, plus anything we will discuss between now and the end of the semester, with the following exceptions:

Using any of these disallowed C features will result in losing credit.

Your program must define three structure types, as described below, which must be named exactly as shown. Your structures must contain the data items specified, but can have additional data fields besides these if you want.


Your program must contain (and use) at least eight separate structure functions. A structure function either has a structure as a parameter, a structure as its return value, or has a pointer to a structure as a parameter. Your program must have at least one such function for each of the three required structures. To get more practice using functions with structures, and to make your program easier to develop and test, it is highly recommended that you write more than eight structure functions (that you write as many as possible). At least two of your structure functions must use call-by-value for their structure parameter(s) or must have a structure as their return value. At least two of the other functions must use call-by-reference for their structures, so they must have pointers to structures as parameters, and they access the structures through the pointers.

If your program doesn't contain, and call, at least eight structure functions, of the types indicated, it will be graded as if it does not work on the primary input- even if its output is correct.

You may also write functions which don't operate on structures, as you like.

Here are some suggestions for possible structure functions; feel free to use others:

Your program must have a comment near the top which contains your name, login ID, student ID, your section number, your TA's name, and an original description of the action and operation of the program. Do not put your alias in this comment! In addition, you must have a comment before each function, explaining its action and operation. Your program should be written using good programming style and formatting, as discussed in class and throughout your textbook. For this project, style is considered to consist of:

4. Developing your program

You may want to skip this section at first, read the rest of the project, and come back to study it carefully when you are about to begin writing your program.

The more you write functions to perform various manipulations on structures, the easier your code becomes to test. You can use either call-by-value or call-by-reference for structure functions, but you have to have the indicated number of functions of each type. It is really recommended you use functions for each separate, discrete operation to be performed on any structure.

Do not use a global structure variable or array, like your book does in its example in Chapter 16! You will lose substantial credit if you use global variables in your project. Use the structure program to be posted as an example of one with structures and functions which doesn't use global variables.

4.1 Possible development steps

It is extremely useful to first develop your program to work for simple cases of input. When it works perfectly for these, you can add code to handle the more complex situations one at a time. For this project, this might mean first developing your program for a simple input data file with just one valid command. You can create several such files, with one command of each type, making sure your results are correct for each. When you have thoroughly tested the different situations which can occur for the various commands and the results for all of them are fine, you can create a larger input file with several commands of different types.

This outline is a suggestion only; you do not have to follow it. There are many ways to develop any program. However, whatever steps you do choose in developing your project, you should be certain to write small parts of your program at a time and test each one to verify that it works before going on!

  1. To get more practice with structures, it might be a good idea to first define just the Ingredient structure type. It contains several fields, but is much simpler than the other structures, which each contain an array of structures. You might also write a function which initializes a variable of this type of structure, using either call-by-value (returning a new structure with appropriate values in its fields) or call-by-reference. Declare a variable of this type, and call this function on it. Store some new values into the appropriate fields of your structure and print the values in all its fields, to verify you are manipulating structures correctly.

  2. Create functions to read and print the information from each of the three input files. Make sure everything is being read and printed correctly before you try to do any comparisons or processing of orders.

  3. Create several simple input files

  4. Go back and read the project description above to identify the other cases and situations which have to be handled. Add code for each of these, compiling and checking after each one.

  5. Lastly, thoroughly check your entire program after finishing it before submitting it.

4.2 Finding compilation errors

If you get an error from the first line of your first structure function, which identifies a syntax error right after the name of the structure type, you probably have declared the structure type after this function which uses it. All structure type definitions must precede the first use of a variable or parameter of that structure type.

Here are several other common compilation errors having to do with structures produced by the cc compiler on our class machines and what they mean:

4.3 Program debugging

  1. Add lots of debug printf statements if your code isn't working, to find out where! You need to know this before you can find out why the problem is occurring.

  2. Draw lots of pictures of your structure types to trace exactly where things are in memory.

  3. If after you have tried these techniques, and tested each of your functions, you still can't figure out why your program doesn't work, bring a printout to our office hours, and we can help you learn how to track the problem down.

4.4 Helpful hints

Frequently save backup copies of your program under different names or in different subdirectories, so even if you inadvertently delete your file or change things which you realize you shouldn't have, it's easy to recover a recent version.

5. Academic integrity statement

Any evidence of unauthorized use of computer accounts or cooperation on projects will be submitted to the Student Honor Council, which could result in an XF for the course, suspension, or expulsion from the University. Projects are to be written INDIVIDUALLY. For academic honesty purposes, projects are to be considered comparable to a take-home exam. Any cooperation or exchange of ideas which would be prohibited on an exam is also prohibited on a project assignment, and WILL BE REPORTED to the Honor Council.


  1. failing to do all or any of the work on a project by yourself, other than assistance from the instructional staff.

  2. using any ideas or any part of another student's project, or copying any other individual's work in any way.

  3. giving any parts or ideas from your project, including test data, to another student.

  4. having programs on an open account or on a PC that other students can access.

  5. transferring any part of a project to or from another student or individual by any means, electronic or otherwise.


6. Submitting your project

Your project must be electronically submitted by the date above, to avoid losing credit as described on the syllabus. No projects more than two days late will be accepted for credit without prior permission or a valid medical excuse, as described on your syllabus. Only the project which you electronically submit, according to the procedures provided, can be graded; it is your responsibility to test your program and verify that it works properly before submitting. Lost passwords or other system problems do not constitute valid justifications for late projects, so do not put off working on your program or wait to submit it at the last minute!

Turn in your assignment using the ``submit'' program as before, except using ``7'' for the project number. You are to submit only the .c file containing your source code, not the executable version of your program! If your program is in a file named ``bakery.c'', submit would be run as submit 7 bakery.c.

Before you submit your project, you must exactly follow the specific submission checklist in the ``Testing projects before submitting'' handout separately posted by your instructor!

7. Sample output

Be sure to test your program against a variety of inputs, so you are sure it works in all circumstances!

The primary input and output files will be posted in your instructor's class posting account- do not try to type them in yourself! The primary output consists of all the data values in the entire output shown in the posted file. Note that this primary input does not exercise a number of conditions discussed above which your program should work for in order to earn credit for the secondary inputs.

Steve Scolnik