I made the code but there has some mistakes, it must show the total the amount o
ID: 3778654 • Letter: I
Question
I made the code but there has some mistakes, it must show the total the amount of money put into the machine at the end of programming and how many dollar bills, quarters, dimes, and nickels.
Could you code a function that display total amount of money how many coins at the end of program?
------------------------------------------------------------------------------------------------------------------------------------------------
You are going to build a simulator for a vending machine.
The machine holds 8 different items:
Qty Item Cost
5 Candy Bar $0.60
5 Energy Bar $1.85
5 Energy Drink $1.70
5 Hard Candy $0.90
5 Popcorn $0.75
5 Potato Chips $1.10
5 Soda $1.45
5 Water $1.25
The machine starts with 10 quarters, 20 dimes, and 30 nickels.
1. At the end of the program, total the amount of money put into the machine, and how many dollar bills, quarters, nickels, and dimes there are in the machine.
2. At the end of the program, write a text file of the items sold, and the total
(i.e. all of 3 and 4 into a text file.) The text file should be named GroupNameVendingMachine.txt (where GroupName is your group’s name)
---------------------------------------------------------------------
//Vending Machine Program
#include<iostream>
#include<fstream>
usingnamespace std;
int main()
{
double product1 = 0.60;
double product2 = 1.85;
double product3 = 1.70;
double product4 = 0.90;
double product5 = 0.75;
double product6 = 1.10;
double product7 = 1.45;
double product8 = 1.25;
double customerMoney = 0.00;
double expense = 0.00;
ofstream outfile;
outfile.open("GroupNameVendingMachine.txt");
cout <<"How much money do you have?: "<<"$";
cin >> customerMoney;
while (customerMoney >= 59.99)
{
int customerChoice = 0;
int exitProgram = 0;
cout <<" Which product would you like to buy?"<<endl;
cout <<"*************************************************";
cout <<" Enter '1' for Candy Bar: ($0.60) Enter '2' for Energy Bar: ($1.85) Enter '3' for Energy Drink: ($1.70) Enter '4' for Hard Candy: ($0.90) Enter '5' for Popcorn: ($0.75) Enter '6' for Potato Chips: ($1.10) Enter '7' for Soda: ($1.45) Enter '8' for Water: ($1.25) Enter '9' for Exit! ";
cout <<"*************************************************"<<endl;
cin >> customerChoice;
if (customerChoice == 1)
{
cout <<"You have selected Candy Bar: ";
customerMoney = customerMoney - product1;
expense = expense - product1;
outfile <<"Candy Bar "<< product1 << endl;
cout <<"You have $"<< customerMoney <<" left ";
}
elseif (customerChoice == 2)
{
cout <<"You have selected Energy Bar: ";
customerMoney = customerMoney - product2;
expense = expense - product2;
outfile <<"Energy Bar "<< product2 << endl;
cout <<"You have $"<< customerMoney <<" left ";
}
elseif (customerChoice == 3)
{
cout <<"You have selected Energy Drink: ";
customerMoney = customerMoney - product3;
expense = expense - product3;
outfile <<"Energy Drink "<< product3 << endl;
cout <<"You have $"<< customerMoney <<" left ";
}
elseif (customerChoice == 4)
{
cout <<"You have selected Hard Candy: ";
customerMoney = customerMoney - product4;
expense = expense - product4;
outfile <<"Hard Candy "<< product4 << endl;
cout <<"You have $"<< customerMoney <<" left ";
}
elseif (customerChoice == 5)
{
cout <<"You have selected Popcorn: ";
customerMoney = customerMoney - product5;
expense = expense - product5;
outfile <<"Popcorn "<< product5 << endl;
cout <<"You have $"<< customerMoney <<" left ";
}
elseif (customerChoice == 6)
{
cout <<"You have selected Potato Chips: ";
customerMoney = customerMoney - product6;
expense = expense - product6;
outfile <<"Potato Chips "<< product6 << endl;
cout <<"You have $"<< customerMoney <<" left ";
}
elseif (customerChoice == 7)
{
cout <<"You have selected Soda: ";
customerMoney = customerMoney - product7;
expense = expense - product7;
outfile <<"Soda "<< product7 << endl;
cout <<"You have $"<< customerMoney <<" left ";
}
elseif (customerChoice == 8)
{
cout <<"You have selected Water: ";
customerMoney = customerMoney - product8;
expense = expense - product8;
outfile <<"Water "<< product8 << endl;
cout <<"You have $"<< customerMoney <<" left ";
}
elseif (customerChoice == 9)
{
outfile <<" total "<< expense << endl;
break;
}
}
cin.ignore();
cin.get();
return 0;
}
Explanation / Answer
Please follow the code and comments for description :
CODE :
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
// function
int exitP(int x){
cout<<" Would you like to Exit the Program? [1 -> yes :: 2 -> no]: " ;
cin>>x;
switch(x){
case 1:
cout<<"Goodbye!";
exit(EXIT_SUCCESS);
break;
case 2:
return x;
break;
default:
cout<<"Please Enter the Numbers 1-2 only. ";
exitP(x);
break;
}
}
int main()
{
double product1 = 0.60;
double product2 = 1.85;
double product3 = 1.70;
double product4 = 0.90;
double product5 = 0.75;
double product6 = 1.10;
double product7 = 1.45;
double product8 = 1.25;
double customerMoney = 0.00;
double expense = 0.00;
int exitProgram = 1;
int quarters = 10;
int dimes = 20;
int nickels = 30;
int dollarBills = 0;
ofstream outfile;
outfile.open("GroupNameVendingMachine.txt");
cout <<"How much money do you have?: "<<"$";
cin >> customerMoney;
while (customerMoney >= 59.99 && exitProgram == 1)
{
int customerChoice = 0;
cout <<" Which product would you like to buy?"<<endl;
cout <<"*************************************************";
cout <<" Enter '1' for Candy Bar: ($0.60) Enter '2' for Energy Bar: ($1.85) Enter '3' for Energy Drink: ($1.70) Enter '4' for Hard Candy: ($0.90) Enter '5' for Popcorn: ($0.75) Enter '6' for Potato Chips: ($1.10) Enter '7' for Soda: ($1.45) Enter '8' for Water: ($1.25) Enter '9' for Exit! ";
cout <<"*************************************************"<<endl;
cin >> customerChoice;
if (customerChoice == 1)
{
cout <<"You have selected Candy Bar: ";
customerMoney = customerMoney - product1;
expense = expense + product1;
outfile <<"Candy Bar "<< product1 << endl;
cout <<"You have $"<< customerMoney <<" left ";
quarters = quarters + 2;
dimes = dimes + 1;
exitP(exitProgram);
}
else if (customerChoice == 2)
{
cout <<"You have selected Energy Bar: ";
customerMoney = customerMoney - product2;
expense = expense + product2;
outfile <<"Energy Bar "<< product2 << endl;
cout <<"You have $"<< customerMoney <<" left ";
dollarBills = dollarBills + 1;
quarters = quarters + 3;
dimes = dimes + 1;
exitP(exitProgram);
}
else if (customerChoice == 3)
{
cout <<"You have selected Energy Drink: ";
customerMoney = customerMoney - product3;
expense = expense + product3;
outfile <<"Energy Drink "<< product3 << endl;
cout <<"You have $"<< customerMoney <<" left ";
dollarBills = dollarBills + 1;
quarters = quarters + 2;
dimes = dimes + 2;
exitP(exitProgram);
}
else if (customerChoice == 4)
{
cout <<"You have selected Hard Candy: ";
customerMoney = customerMoney - product4;
expense = expense + product4;
outfile <<"Hard Candy "<< product4 << endl;
cout <<"You have $"<< customerMoney <<" left ";
quarters = quarters + 3;
dimes = dimes + 1;
nickels = nickels + 1;
exitP(exitProgram);
}
else if (customerChoice == 5)
{
cout <<"You have selected Popcorn: ";
customerMoney = customerMoney - product5;
expense = expense + product5;
outfile <<"Popcorn "<< product5 << endl;
cout <<"You have $"<< customerMoney <<" left ";
quarters = quarters + 3;
exitP(exitProgram);
}
else if (customerChoice == 6)
{
cout <<"You have selected Potato Chips: ";
customerMoney = customerMoney - product6;
expense = expense + product6;
outfile <<"Potato Chips "<< product6 << endl;
cout <<"You have $"<< customerMoney <<" left ";
dollarBills = dollarBills + 1;
dimes = dimes + 1;
exitP(exitProgram);
}
else if (customerChoice == 7)
{
cout <<"You have selected Soda: ";
customerMoney = customerMoney - product7;
expense = expense + product7;
outfile <<"Soda "<< product7 << endl;
cout <<"You have $"<< customerMoney <<" left ";
dollarBills = dollarBills + 1;
quarters = quarters + 1;
dimes = dimes + 2;
exitP(exitProgram);
}
else if (customerChoice == 8)
{
cout <<"You have selected Water: ";
customerMoney = customerMoney - product8;
expense = expense + product8;
outfile <<"Water "<< product8 << endl;
cout <<"You have $"<< customerMoney <<" left ";
dollarBills = dollarBills + 1;
quarters = quarters + 1;
exitP(exitProgram);
}
else if (customerChoice == 9)
{
outfile <<" total "<< expense << endl;
break;
}
}
cout << "Total amount of money put into the machine is : " << expense << endl;
cout << "Money Denominations : " << endl;
cout << "Dollar Bills : " << dollarBills << endl;
cout << "Quarters : " << quarters << endl;
cout << "Dimes : " << dimes << endl;
cout << "Nickels : " << nickels << endl;
cin.ignore();
cin.get();
return 0;
}
OUTPUT :
How much money do you have?: $90
Which product would you like to buy?
*************************************************
Enter '1' for Candy Bar: ($0.60)
Enter '2' for Energy Bar: ($1.85)
Enter '3' for Energy Drink: ($1.70)
Enter '4' for Hard Candy: ($0.90)
Enter '5' for Popcorn: ($0.75)
Enter '6' for Potato Chips: ($1.10)
Enter '7' for Soda: ($1.45)
Enter '8' for Water: ($1.25)
Enter '9' for Exit!
*************************************************
1
You have selected Candy Bar: You have $89.4 left
Would you like to Exit the Program? [1 -> yes :: 2 -> no]: 2
Which product would you like to buy?
*************************************************
Enter '1' for Candy Bar: ($0.60)
Enter '2' for Energy Bar: ($1.85)
Enter '3' for Energy Drink: ($1.70)
Enter '4' for Hard Candy: ($0.90)
Enter '5' for Popcorn: ($0.75)
Enter '6' for Potato Chips: ($1.10)
Enter '7' for Soda: ($1.45)
Enter '8' for Water: ($1.25)
Enter '9' for Exit!
*************************************************
2
You have selected Energy Bar: You have $87.55 left
Would you like to Exit the Program? [1 -> yes :: 2 -> no]: 2
Which product would you like to buy?
*************************************************
Enter '1' for Candy Bar: ($0.60)
Enter '2' for Energy Bar: ($1.85)
Enter '3' for Energy Drink: ($1.70)
Enter '4' for Hard Candy: ($0.90)
Enter '5' for Popcorn: ($0.75)
Enter '6' for Potato Chips: ($1.10)
Enter '7' for Soda: ($1.45)
Enter '8' for Water: ($1.25)
Enter '9' for Exit!
*************************************************
3
You have selected Energy Drink: You have $85.85 left
Would you like to Exit the Program? [1 -> yes :: 2 -> no]: 2
Which product would you like to buy?
*************************************************
Enter '1' for Candy Bar: ($0.60)
Enter '2' for Energy Bar: ($1.85)
Enter '3' for Energy Drink: ($1.70)
Enter '4' for Hard Candy: ($0.90)
Enter '5' for Popcorn: ($0.75)
Enter '6' for Potato Chips: ($1.10)
Enter '7' for Soda: ($1.45)
Enter '8' for Water: ($1.25)
Enter '9' for Exit!
*************************************************
4
You have selected Hard Candy: You have $84.95 left
Would you like to Exit the Program? [1 -> yes :: 2 -> no]: 2
Which product would you like to buy?
*************************************************
Enter '1' for Candy Bar: ($0.60)
Enter '2' for Energy Bar: ($1.85)
Enter '3' for Energy Drink: ($1.70)
Enter '4' for Hard Candy: ($0.90)
Enter '5' for Popcorn: ($0.75)
Enter '6' for Potato Chips: ($1.10)
Enter '7' for Soda: ($1.45)
Enter '8' for Water: ($1.25)
Enter '9' for Exit!
*************************************************
5
You have selected Popcorn: You have $84.2 left
Would you like to Exit the Program? [1 -> yes :: 2 -> no]: 2
Which product would you like to buy?
*************************************************
Enter '1' for Candy Bar: ($0.60)
Enter '2' for Energy Bar: ($1.85)
Enter '3' for Energy Drink: ($1.70)
Enter '4' for Hard Candy: ($0.90)
Enter '5' for Popcorn: ($0.75)
Enter '6' for Potato Chips: ($1.10)
Enter '7' for Soda: ($1.45)
Enter '8' for Water: ($1.25)
Enter '9' for Exit!
*************************************************
6
You have selected Potato Chips: You have $83.1 left
Would you like to Exit the Program? [1 -> yes :: 2 -> no]: 2
Which product would you like to buy?
*************************************************
Enter '1' for Candy Bar: ($0.60)
Enter '2' for Energy Bar: ($1.85)
Enter '3' for Energy Drink: ($1.70)
Enter '4' for Hard Candy: ($0.90)
Enter '5' for Popcorn: ($0.75)
Enter '6' for Potato Chips: ($1.10)
Enter '7' for Soda: ($1.45)
Enter '8' for Water: ($1.25)
Enter '9' for Exit!
*************************************************
7
You have selected Soda: You have $81.65 left
Would you like to Exit the Program? [1 -> yes :: 2 -> no]: 2
Which product would you like to buy?
*************************************************
Enter '1' for Candy Bar: ($0.60)
Enter '2' for Energy Bar: ($1.85)
Enter '3' for Energy Drink: ($1.70)
Enter '4' for Hard Candy: ($0.90)
Enter '5' for Popcorn: ($0.75)
Enter '6' for Potato Chips: ($1.10)
Enter '7' for Soda: ($1.45)
Enter '8' for Water: ($1.25)
Enter '9' for Exit!
*************************************************
8
You have selected Water: You have $80.4 left
Would you like to Exit the Program? [1 -> yes :: 2 -> no]: 2
Which product would you like to buy?
*************************************************
Enter '1' for Candy Bar: ($0.60)
Enter '2' for Energy Bar: ($1.85)
Enter '3' for Energy Drink: ($1.70)
Enter '4' for Hard Candy: ($0.90)
Enter '5' for Popcorn: ($0.75)
Enter '6' for Potato Chips: ($1.10)
Enter '7' for Soda: ($1.45)
Enter '8' for Water: ($1.25)
Enter '9' for Exit!
*************************************************
9
Total amount of money put into the machine is : 9.6
Money Denominations :
Dollar Bills : 5
Quarters : 25
Dimes : 28
Nickels : 31
GroupNameVendingMachine.txt
Candy Bar 0.6
Energy Bar 1.85
Energy Drink 1.7
Hard Candy 0.9
Popcorn 0.75
Potato Chips 1.1
Soda 1.45
Water 1.25
Total 9.6
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.