Write a C++ program to simulate a vending machine using the switch statement. It
ID: 3887525 • Letter: W
Question
Write a C++ program to simulate a vending machine using the switch statement. It is stocked with the following items.
A - Hershey Bar Plain, $0.60
B - Hershey with Almonds, $0.65
C - M&Ms, $0.50
D - Trail Mix, $1.00
E - Milky Way, $0.95
Assume the user inputs one dollar. Provide the user with a menu list which must look like the one above. Ask the user for the letter corresponding to the item they want. Let the user know the item was dispensed and how much is in the change dispenser. An example of the user purchasing a bag of M&Ms might look like:
Welcome to the Dewy, Cheatum, and Howe Vending machine. You have input $1, please make a selection by inputting the character preceding the item. C
Your item has been dispensed, please enjoy!
You have 50 cents in the change dispenser.
Would you like to make another purchase, (y/n) ?
Then use a while loop to repeatedly use the program.
Explanation / Answer
// C++ code
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <fstream>
#include <stdlib.h>
#include <vector>
#include <string>
using namespace std;
void menu()
{
cout << " A - Hershey Bar Plain, $0.60 B - Hershey with Almonds, $0.65 C - M&Ms, $0.50 D - Trail Mix, $1.00 E - Milky Way, $0.95 ";
}
int main()
{
cout << "Welcome to the Dewy, Cheatum, and Howe Vending machine. ";
char input;
char again;
double amount = 100;
while(true)
{
menu();
cout << " You have input $1, please make a selection by inputting the character preceding the item: ";
cin >> input;
cout << "Your item has been dispensed, please enjoy! ";
switch(input)
{
case 'A':
cout << "You have " << (amount-0.6*100) << " cents in the change dispenser. ";
break;
case 'B':
cout << "You have " << (amount-0.65*100) << " cents in the change dispenser. ";
break;
case 'C':
cout << "You have " << (amount-0.5*100) << " cents in the change dispenser. ";
break;
case 'D':
cout << "You have " << (amount-1*100) << " cents in the change dispenser. ";
break;
case 'E':
cout << "You have " << (amount-0.95*100) << " cents in the change dispenser. ";
break;
default:
cout << "Invalid input ";
break;
}
cout << "Would you like to make another purchase, (y/n)? ";
cin >> again;
if(again == 'n')
break;
}
return 0;
}
/*
output:
Welcome to the Dewy, Cheatum, and Howe Vending machine.
A - Hershey Bar Plain, $0.60
B - Hershey with Almonds, $0.65
C - M&Ms, $0.50
D - Trail Mix, $1.00
E - Milky Way, $0.95
You have input $1, please make a selection by inputting the character preceding the item: A
Your item has been dispensed, please enjoy!
You have 40 cents in the change dispenser.
Would you like to make another purchase, (y/n)? y
A - Hershey Bar Plain, $0.60
B - Hershey with Almonds, $0.65
C - M&Ms, $0.50
D - Trail Mix, $1.00
E - Milky Way, $0.95
You have input $1, please make a selection by inputting the character preceding the item: B
Your item has been dispensed, please enjoy!
You have 35 cents in the change dispenser.
Would you like to make another purchase, (y/n)? y
A - Hershey Bar Plain, $0.60
B - Hershey with Almonds, $0.65
C - M&Ms, $0.50
D - Trail Mix, $1.00
E - Milky Way, $0.95
You have input $1, please make a selection by inputting the character preceding the item: D
Your item has been dispensed, please enjoy!
You have 0 cents in the change dispenser.
Would you like to make another purchase, (y/n)? n
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.