robbydooby

Vending Machine Program

Jan 24th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  vendingmachine
  4. //
  5. //  This program simulates a vending machine.
  6. //
  7. //  Created by Robert Doobay on 1/24/17.
  8. //  Copyright © 2017 Robert Doobay. All rights reserved.
  9. //
  10.  
  11. #include <stdio.h>
  12.  
  13. int main()
  14. {
  15.    
  16.     int coin = 1;   // Set to 1 to allow input cash loop to begin
  17.     int choice = 4; // Set to 4 to allow input menu loop to begin
  18.     int cash = 0;
  19.    
  20.     //  Welcome Statements
  21.     printf("Welcome to Vending Machine 1.0\n");
  22.     printf("Designed by Robert Doobay\n\n");
  23.     printf("Please enter coins by entering the value of each coin.\n");
  24.     printf("Coin values can be either 5, 10, or 25.\n");
  25.    
  26.     //  Input Cash Loop
  27.     while (coin != 0)
  28.     {
  29.         //  Queries input from user
  30.         printf("Insert Coins:");
  31.        
  32.         //  Validates input from user
  33.             // Checks if input is numeric
  34.         if (scanf("%d", &coin) != 1) printf("This is not a number, please try again.\n");
  35.             //  Checks if input is a valid coin value
  36.         else if (coin == 5 || coin == 10 || coin == 25 || coin == 0) cash+= coin;
  37.             //  Throws error if illegal numeric value is entered
  38.         else printf("Illegal coin value entered, please try again.\n");
  39.        
  40.         // Flushes input buffer
  41.         while (getchar() != '\n'){}
  42.     }
  43.    
  44.    
  45.     //  Thanks user and displays cash entered
  46.     printf("\nCoins accepted, thank you.\n");
  47.     if (cash%100 != 0) printf("Total Cash: $%d.%d\n\n", cash/100, cash%100);
  48.     else printf("Total Cash: $%d.00\n\n", cash/100);
  49.    
  50.     //  Input Menu Loop
  51.     printf("Please enter the corresponding number of the desired item from the following menu:\n");
  52.     printf("1: Coffee   25 cents\n2: Tea      15 cents\n0: Return Cash\n\n");
  53.     printf("Choice: ");
  54.    
  55.     while (choice != (0 | 1 | 2))
  56.     {
  57.         //  Validates Input
  58.         if (scanf("%d", &choice) != 1) printf("This is not a number. Please try again.\n");
  59.        
  60.         //  Serves Coffee, Tea, Returns Cash, or throws error if illegal input is entered.
  61.         else switch (choice)
  62.         {
  63.             case 1:
  64.                 cash-= 25;
  65.                 printf("Serving Coffee...\nYour change is $%d.%d\n", cash/100, cash%100);
  66.                 break;
  67.             case 2:
  68.                 cash-= 15;
  69.                 printf("Serving Tea...\nYour change is $%d.%d\n", cash/100, cash%100);
  70.                 break;
  71.             case 0:
  72.                 cash = 0;
  73.                 printf("Your cash has been returned.\n");
  74.                 break;
  75.             default:
  76.                 printf("Illegal value entered, please try again.\n");
  77.                 // Flushes input buffer
  78.                 while (getchar() != '\n'){}
  79.                 printf("Choice: ");
  80.                 break;
  81.         }
  82.        
  83.     }
  84.    
  85.    
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment