CS 010 - Introduction to Computer Science I
Lab 9. Structures, Menus

Points(10 overall)

Collaboration policy:

Collaboration on this lab exercise is strongly ENCOURAGED. The exercise is intended for practice, not assessment -- most people who try should get all participation points. Feel free to ask for help from, and provide help to, others. You shouldn't blindly copy solutions from one another (or from anywhere else) nor simply write code for someone else (even if you explain it as you write), but you can certainly help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc. If you get done early, feel free to walk around and help others.


Part 1

Choose a partner to work with on this lab

With your partner, define a structure of type Product. The member variables should be code (int), wholesale_price (double), retail_price (double), number (int), and reorder_val (int).

Also with your partner discuss the pre and post-conditions for the following two functions. Then one partner should write the first function, initProduct, and the other partner should write the second function, outputProduct.


initProduct function

Write the definition of an initiation function:

       void initProduct(Product& new_product)

This function should initialize all member variables of the passed Product object.
The function should prompt the user for the product code, the wholesale price, the number in stock, and the reorder value for this product.
The function should only allow the user to enter values between 1000 and 9999 for the product code.
All products have a 10% markup, so the function should calculate the retail price as 1.1 times the wholesale price.

Write a driver program to test this function.


outputProduct function

Write the definition of an output function:

       void outputProduct(Product new_product)

This function should output your Product information in the following format:

code    W cost    R cost  # in stock    Reorder #
----   --------  -------- ----------    ---------

1111   $  10.00  $  11.00         15           50
2222   $ 100.00  $ 110.00        100           10
3333   $   5.00  $   5.50      12000          100

Your output function should just output one Product object. So, the preceding example would need 3 calls to this function to output the 3 products (1111, 2222, 3333). So, the column headings would need to be output before these function calls. You could do this with a function that just outputs the heading.

Write a driver program that uses a stub for the initProduct function to test this output function.


Main function

Once the two functions have been tested and you feel confident they work correctly, you and your partner should now work on the main together that uses their function along with your function. If you get done testing your function before your partner, you should help them finish testing their function before starting on the main function.

Your program should prompt the user to enter info for 3 products. Then output the inventory in the above format. That means for this part you will declare 3 objects of type Product.


Part 2 (work on Part 2 and Part 3 together with your partner)

Modify your program so that it allows the user to enter as many products as they want and make it print the output to a file called inventory.dat. Output to this file only the raw data (numbers separated by spaces). You should only declare one object of type Product and put your functions in a do-while loop that asks the user if they want to continue entering products.


Part 3

Modify your program to create reorder.dat. The program should output to the file, reorder.dat, all items that need to be reordered. A product needs to be reordered if its # in stock is lower than its reorder #. In the example output of Part 1, product 1111 would need to be reordered since its # in stock, 15, is lower than its reorder #, 50.


Part 4

Make a program with a menu of choices for the user. This menu should give the user the following options:

  1. Print a rectangle
  2. Print an upper equilateral triangle
  3. Print a lower equilateral triangle
  4. Print a plus sign
  5. Exit the program

Your program should output these shapes using the * character. Each menu choice should call a function to print the appropriate shape. For the rectangle, your function should ask the user for the width and length. For the triangles and the plus sign, the program should ask the user for the length of a side. Design the menu and then get one function working at a time. The untested branches of the menu should just output some test message like "choice 3" and then bring up the menu again.

The following are examples of each shape:


Challenge Exercises:

Please feel free to help your fellow labmates reach this point!!

The following exercises are for your own benefit. You are not required to do these. In fact, they may contain material not covered in class or lab yet. As such, the TAs will give priority to helping students on the previous sections.


Challenge 1: