CS2020 Program 2
Your assignment is to create a program that simulates a vending machine. Be sure to store your program in your class account on bgunix. Use the name pgm2.cpp for your program file.
1. Declare a structure (e.g., Snack) with the data members snack name, cost and quantity. The struct should be declared before your main function and your function prototypes.
The steps below should be included in your main function.
2. Declare an array of five Snack structures, each one initialized with the values shown in the table below. Initialize the data members of each struct with the name of the snack, the cost of the snack and the quantity of the snack available in the vending machine.
Snack Cost Quantity
Reeses 1.00 4
M&Ms 1.00 2
Oreos 0.75 3
Doritos 0.50 3
Cheetos 0.75 2
An array of structs must be used to get credit for this assignment. See page 613 in the Gaddis textbook for an example of how to define an array of structs AND initialize the values of each element at the same time. Note that an initialization list can be used ONLY at the time you define the array.
3. Call a function to display a menu of options and accept a selection number from the user. The function accepts the array of structs as an argument, displays a list of the snacks and the cost of each, asks the user for a selection, validates the selection (using a validation loop) and returns the selection to the main function.
4. Create a while loop that continues until the user selection returned by the function in the previous step is 6 to end the snack machine program. In this loop:
a. Call a function that accepts the array of structs, the user`s menu choice and the total amount earned from transactions on this machine. This function should:
• Check to make sure the selected snack is in stock. Print a message if it is not and return.
• If the snack is in stock, get the amount of money entered by the user to pay for the snack.
• Use a validation loop to make sure the amount of money entered by the user is valid (do not accept negative values, values greater than $1.00 or values less than the cost of the snack).
• Calculate any change due and display it.
• Update the amount earned from snack sales.
• Update the inventory amount for the snack selected.
b. Call the function to get the user`s selection again to allow the user to enter a new selection.
- Reeses 1.00
- M&Ms 1.00
- Oreos 0.75
- Doritos 0.50
- Cheetos 0.75
- End snack program
Please make your selection: 3
Enter payment amount: 1.00
Enjoy your Oreos snack!
Your change is 0.25
5. After the loop terminates, call a function to display the list of snacks, the cost of each and the quantity of each left. Also display the total amount earned for this vending machine before ending the program. Program Documentation & Style
1. Include function prototypes before the main function. Declare all constants and variables that your program uses at the beginning of the main function.
2. Your program should include three types of comments. Use complete sentences. BE SPECIFIC!
a. Header comments at the beginning of your program including lines with:
- Your name, course name, and class time
- Program assignment number, file name (e.g., pgm2.cpp) and due date
- Purpose: a sentence or two explaining the purpose of the program
- Input/Data: a sentence or two describing the input or initial data used by the program when you run it
- Processing: a few sentences describing the processing and/or calculations done by the program
- Output: a description of the results (output) produced by the program
b. In-line comments: There should be an in-line comment for each main step in your program. In general, this means at least one comment with each group of C++ statements that handle the input, the processing and the output steps of your program.
c. Function comments. A sentence or two should be included before each function definition to explain its purpose.
3. Use meaningful identifier names.
4. Include clear prompts for the user about entering the data when appropriate.
5. Include clear, descriptive labels for output values.
6. Format your output neatly.
Turning in your programs
- Make sure you have completed your program according to the specifications given.
- Make a photo of your program by typing in the following commands at the prompt:
$ photo pgm2.log ( to start the photo utility)
$ pwd ( to identify your current directory)
$ ls -l ( to list all files in your directory – lower case Ls not ones)
$ ( to leave a few blank lines press the Enter key )
$ cat pgm2.cpp ( to show your C++ source code)
$ g++ pgm2.cpp ( to compile your program - creates a.out file)
$ a.out Enter the input values below when you run your program:
Selection Amount paid
$ exit ( to end the photo session)
CS2020 Grading Rubric - Program 2
_______ Files named correctly (pgm2.cpp, pgm2.log) and in your class directory by the due time. All of the commands shown must be typed in during the photo session. Partial credit is given so be sure your photo log file is in your class account on bgunix by the due time even if the program is not completed. No credit given if program is turned in late.
_______ (2) Header comments complete, includes labeled sections identifying the Purpose, Input, Processing and Output
_______ (2) In-line/Function comments complete, uses good program style (meaningful data names, whitespace, indentation)
_______ (2) main function creates array of structs and includes sentinel-controlled loop to allow user to enter choices until done
_______ (3) Function to get user`s selection correctly displays snack list menu, gets user selection and validates it, returns selection to main function. Data aligned neatly in columns.
_______ (3) Function to process user`s selection checks for availability of snack, validates amount entered, calculates change, updates amount earned and quantity left
_______ (3) Function to display the snack list shows correct final quantities and total amount earned
_______ (15) Total Points