Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // vendingmachine
- //
- // This program simulates a vending machine.
- //
- // Created by Robert Doobay on 1/24/17.
- // Copyright © 2017 Robert Doobay. All rights reserved.
- //
- #include <stdio.h>
- int main()
- {
- int coin = 1; // Set to 1 to allow input cash loop to begin
- int choice = 4; // Set to 4 to allow input menu loop to begin
- int cash = 0;
- // Welcome Statements
- printf("Welcome to Vending Machine 1.0\n");
- printf("Designed by Robert Doobay\n\n");
- printf("Please enter coins by entering the value of each coin.\n");
- printf("Coin values can be either 5, 10, or 25.\n");
- // Input Cash Loop
- while (coin != 0)
- {
- // Queries input from user
- printf("Insert Coins:");
- // Validates input from user
- // Checks if input is numeric
- if (scanf("%d", &coin) != 1) printf("This is not a number, please try again.\n");
- // Checks if input is a valid coin value
- else if (coin == 5 || coin == 10 || coin == 25 || coin == 0) cash+= coin;
- // Throws error if illegal numeric value is entered
- else printf("Illegal coin value entered, please try again.\n");
- // Flushes input buffer
- while (getchar() != '\n'){}
- }
- // Thanks user and displays cash entered
- printf("\nCoins accepted, thank you.\n");
- if (cash%100 != 0) printf("Total Cash: $%d.%d\n\n", cash/100, cash%100);
- else printf("Total Cash: $%d.00\n\n", cash/100);
- // Input Menu Loop
- printf("Please enter the corresponding number of the desired item from the following menu:\n");
- printf("1: Coffee 25 cents\n2: Tea 15 cents\n0: Return Cash\n\n");
- printf("Choice: ");
- while (choice != (0 | 1 | 2))
- {
- // Validates Input
- if (scanf("%d", &choice) != 1) printf("This is not a number. Please try again.\n");
- // Serves Coffee, Tea, Returns Cash, or throws error if illegal input is entered.
- else switch (choice)
- {
- case 1:
- cash-= 25;
- printf("Serving Coffee...\nYour change is $%d.%d\n", cash/100, cash%100);
- break;
- case 2:
- cash-= 15;
- printf("Serving Tea...\nYour change is $%d.%d\n", cash/100, cash%100);
- break;
- case 0:
- cash = 0;
- printf("Your cash has been returned.\n");
- break;
- default:
- printf("Illegal value entered, please try again.\n");
- // Flushes input buffer
- while (getchar() != '\n'){}
- printf("Choice: ");
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment