Homework 1
Caution
  • You are expected to work individually.
  • Due: Wednesday, June 15th at 11pm EST (Baltimore time).
  • This assignment is worth 60 points.

Learning Objectives

Objectives
  • arithmetic operators
  • control structures
  • input collection and validation
  • version control using git

Overview

In this assignment, you will create a C program that simulates a vending machine. The machine can sell cola for 75 cents, a candy bar for $1.25, and popcorn for 50 cents. At the start, the machine holds five of each item.

The program will begin by showing the user a welcome message, instructions and a menu, showing the items available (including how many are left in stock) and their prices. The program will then read the purchase choice represented as an integer corresponding to the desired menu entry and the amount of money inserted into the machine (formatted as dollars and cents).

If the choice is valid, it is in stock, and sufficient money was entered, the program will output that the item is dispensed and display the change returned to the user. At this point, the program should also display the remaining count for each item as well as the total money the machine earned so far. As long as the input is valid, the program should continue to run until the user chooses to quit. If the choice is not valid, the item is sold out, or insufficient money was inserted, the program should output an appropriate error according to the specifications below. Your output must match the sample runs below exactly with respect to these messages, spacing, etc.

The program should read user data from standard input and output the results to standard output. If there is an error in the format of the user input, then you will identify the error type and print an appropriate message and end the program. See the details and Example Runs section below.

Program Requirements

Tip

In some situations, the user will need to press ctrl-d` twice in a row.

Development Requirements

In the homework folder of your private repository (2023-summer-student-JHED), you should create a new subfolder named hw1. In that hw1 subfolder, you will create your program in a new C source file named hw1.c. At the top of the file, add a comment with your six character alphanumeric Hopkins ID. (Please do not include your name or JHED so as to allow for blind grading.)

Throughout your work on this assignment, be sure to frequently add, commit (supplying meaningful messages) and push your changes to your personal git repository. After you complete your work on the assignment, you will be asked to submit a gitlog.txt file, just as in Homework 0. However, we expect your log for this homework to show more activity.

Recall that your code is always expected to compile without errors or warnings, on the ugrad servers. Submissions which do not compile properly may earn zero points, so be sure to submit to Gradescope early and often! And once you get a good start on the assignment, always have some earlier compiling version of your work pushed up to Github.

Example Runs

Here are several samples runs of the program on ugrad, where $ denotes the command prompt, and user input is shown in bold. Note that the first line shown below is the command you are expected to use as you compile your program (and the one that will be used by the graders). The compilation line should report zero errors and warnings, as demonstrated below:

Example 1

$ gcc -std=c99 -pedantic -Wall -Wextra hw1.c
$ ./a.out
Welcome to the Vending Machine!
Enter your choice by # and input cash amount, repeatedly (^d to end).
[0] 5 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $0.00
0 1.00
cola is dispensed and $0.25 returned
[0] 4 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $0.75
(user types ctrl-d) 
Thanks for your patronage!

Example 2

$ ./a.out
Welcome to the Vending Machine!
Enter your choice by # and input cash amount, repeatedly (^d to end).
[0] 5 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $0.00
0 2.00 1 3.00
cola is dispensed and $1.25 returned
[0] 4 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $0.75
candybar is dispensed and $1.75 returned
[0] 4 cola left: cost is $0.75
[1] 4 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $2.00
(user types ctrl-d)
Thanks for your patronage!

Example 3

$ ./a.out
Welcome to the Vending Machine!
Enter your choice by # and input cash amount, repeatedly (^d to end).
[0] 5 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $0.00
0 2 1 3 2 10
cola is dispensed and $1.25 returned
[0] 4 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $0.75
candybar is dispensed and $1.75 returned
[0] 4 cola left: cost is $0.75
[1] 4 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $2.00
popcorn is dispensed and $9.50 returned
[0] 4 cola left: cost is $0.75
[1] 4 candybar left: cost is $1.25
[2] 4 popcorn left: cost is $0.50
Money made so far is $2.50
(user types ctrl-d)
Thanks for your patronage!

Example 4

$ ./a.out
Welcome to the Vending Machine!
Enter your choice by # and input cash amount, repeatedly (^d to end).
[0] 5 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $0.00
cola 1
malformed expression

Example 5

$ ./a.out
Welcome to the Vending Machine!
Enter your choice by # and input cash amount, repeatedly (^d to end).
[0] 5 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $0.00
3 12 
invalid item

Example 6

$ ./a.out
Welcome to the Vending Machine!
Enter your choice by # and input cash amount, repeatedly (^d to end).
[0] 5 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $0.00
2 -12.22
malformed expression

Example 7 (note that the program exits with the first error parsed)

$ ./a.out
Welcome to the Vending Machine!
Enter your choice by # and input cash amount, repeatedly (^d to end).
[0] 5 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $0.00
Mark 0.00
malformed expression

Example 8 (spaces and new lines should be ignored)

$ ./a.out
Welcome to the Vending Machine!
Enter your choice by # and input cash amount, repeatedly (^d to end).
[0] 5 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $0.00
   2                3
popcorn is dispensed and $2.50 returned
[0] 5 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 4 popcorn left: cost is $0.50
Money made so far is $0.50
(user types ctrl-d)
Thanks for your patronage!

Example 9

$ ./a.out
Welcome to the Vending Machine!
Enter your choice by # and input cash amount, repeatedly (^d to end).
[0] 5 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $0.00
2 0.00
Not enough money

Example 10 (pretending cola starts with 1 stock to show what happens when buying from 0 stock)

$ ./a.out
Welcome to the Vending Machine!
Enter your choice by # and input cash amount, repeatedly (^d to end).
[0] 1 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $0.00
0 1
cola is dispensed and $0.25 returned
[0] 0 cola left: cost is $0.75
[1] 5 candybar left: cost is $1.25
[2] 5 popcorn left: cost is $0.50
Money made so far is $0.75
0 2
invalid item
Tip

There may be other ways for the input expressions to be malformed, besides the ways shown above. You must be careful to check for all the various ways it might be malformed.

Submission

Create a .zip file named hw1.zip which contains only hw1.c and gitlog.txt. (Do not zip your entire hw1 folder - only these two files.) Copy the hw1.zip file to your local machine and submit it as Homework 1 on Gradescope.

Recall you can create your gitlog.txt file by running git log > gitlog.txt.

When you submit, Gradescope conducts a series of automatic tests. These tests do basic checks like making sure that you submitted the right files and that your .c file compiles properly. If you see error messages here (look for red), address them and resubmit.

No-compile Policy

Remember that if your final submitted code does not compile, you will earn a zero score for the assignment.

Tip

You may re-submit any number of times prior to the deadline; only your latest submission will be graded.

Info

Review the course syllabus for late submission policies (grace period and late days). You will want to save your late days for the future assignments as they will be more involved.

You should also make sure that your code has good style. You can look at the coding style guidelines here from a course you will take later that also applies to this course. In brief, you should make sure that your submission is well formed:

Two notes regarding automatic checks for programming assignments: