Question

Drink Machine Simulator C++

Drink Machine Simulator

The purpose of this exercise is to give you practice with abstract data types, namely structures and arrays of structures.Write a program that simulates a softdrink machine. The program should use a structure that stores the following data:

Drink Name
Drink Cost
Number of Drinks in Machine

The program should create an array of five structures. The elements should be initialized with the following data:

Drink Name Cost Number in Machine
Coca-Cola .75 20
Root Beer .75 20
Sprite .75 20
Spring Water .80 20
Apple Juice .80 20

Please see the input file ("DrinkMachineInventory.txt"). Each time the program runs, it should read the data from the input file and then enter a loop thatperforms the following steps: A list of drinks is displayed on the screen. The user should be allowed to either quit the program or pick a drink. If the userselects a drink, he or she will next enter the amount of money that is to be inserted into the drink machine. The program should display the amount of change thatwould be returned and subtract one from the number of that drink left in the machine. If the user selects a drink that has sold out, a message should be displayed.The loop then repeats. When the user chooses to quit the program it should display the total amount of money the machine earned.
Input Validation: Only accept positive values for the amount of money. Also, do not accept values greater than $1.00.
Your code should be modular (use functions) and pass variables (by value/by reference) where appropriate. Also, use named constants whereappropriate

DrinkMachineInventory.txt-
Coca-Cola 0.75 20
Root Beer 0.75 20
Sprite 0.75 20
Spring Water 0.80 20
Apple Juice 0.95 20

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C++ program for Drink Machine Simulator:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;

struct Machine
{
string name;
double cost;
int num;
};

void init(Machine []);
int menu(Machine[]);
void payment(double);

int main()
{
Machine drink[5];
int choice;
double made=0;
init(drink);
choice=menu(drink);
while(choice!=5)
{
payment(drink[choice].cost);
made+=drink[choice].cost;
drink[choice].num--;
choice=menu(drink);
}
cout<<"Today the machine has made $"<<setprecision(2)<<fixed<<made<<endl;
system("pause");
return 0;
}

void payment(double p)
{
double pay;
cout<<"Your drink costs $"<<setprecision(2)<<fixed<<p<<endl;
cout<<"Enter payment: ";
cin>>pay;
while(pay<0||pay>1.||pay<p)
{
cout<<"please insert the correct amount for your drink!n";
cout<<"maximum payment is $1.00n";
cout<<"Enter payment: ";
cin>>pay;
}
cout<<"Your change is: $"<<setprecision(2)<<fixed<<pay-p<<endl;
return;
}

void init(Machine d[])
{
ifstream infile("DrinkMachineInventory.txt");

if(infile.fail())
{
cout << "Could not find the file DrinkMachineInventory.txt n";
cout << "Exiting the programn";
exit(0);
}

int i=0;
char ch;
string word= "";

while(!infile.eof())
{
word= "";
ch = infile.get();
while(true)
{
if(isdigit(ch) || ch == 'n')
break;
else
word += ch;
ch = infile.get();
}

if(word != "")
{
d[i].name = word;
infile >> d[i].cost >> d[i].num ;
i++;
}
}

infile.close();
}

int menu(Machine d[])
{
int choice=8,i;
bool soldout=true;
while((choice<1||choice>6)||soldout)
{
soldout=false;
cout<<"Menun";
cout<<" Drink Costtleftn";
for(i=0;i<5;i++)
{
cout<<i+1<<". "<<setw(15)<<left<<d[i].name<<setw(5);
cout<<setprecision(2)<<fixed<<d[i].cost<<"t"<<d[i].num<<endl;
}
cout<<"6. Exitn";
cout<<"Enter Choice ";
cin>>choice;
if(choice<1||choice>6)
cout<<"invalid entryn";
else
if(d[choice-1].num==0)
{cout<<"sold outn";
soldout=true;
}
}
return choice-1;
}

next step

DrinkMachineInventory.txt file:

uploaded image

next step

Sample output:

uploaded image

I hope this would helpful for you......

answered by: Darius
Add a comment
Know the answer?
Add Answer to:
Drink Machine Simulator C++
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • q13 c++ if answering please do not write code in paragrapth form. 13. Drink Machine Simulator...

    q13 c++ if answering please do not write code in paragrapth form. 13. Drink Machine Simulator Write a program that simulates a soft drink machine. The program should use a structure that stores the following data: Drink Name Drink Cost Number of Drinks in Machine The program should create an array of five structures. The elements should be initialized with the following data: Drink Name Cost Number in Machine Cola .75 20 Root Beer .75 20 Lemon-Lime .75 20 Grape...

  • NOTE #1: NO GLOBALS, In addition programs with infinite while(true) loops or improper breaks etc. NOTE...

    NOTE #1: NO GLOBALS, In addition programs with infinite while(true) loops or improper breaks etc. NOTE #2: MAKE YOUR PROGRAM AS EFFICIENT AS POSSIBLE. Drink Machine Simulator - Based on Structures Lecture Write a c++ program that simulates a soft drink machine. The program should use a structure named Drink that contains the following information: the drink name the drink cost the number of drinks in the machine The program should then create an array of 5 Drink structures. The...

  • Prompt 1 Write a program for a simple vending machine. The vending machine contains the following...

    Prompt 1 Write a program for a simple vending machine. The vending machine contains the following set of drinks (indexed from 1 to 5): Coca-Cola, Sprite, Snapple, Dr. Pepper, Water. A variable byte choice will simulate user input (suppose a user is able to input a number < byte's max size). Your program must output the following: "You have chosen %s" where %s is a placeholder for the name of the drink choser. Prompt 2: Write a program that checks...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

  • 1. Create a new multi-class Java program which implements a vending machine simulator which contains the...

    1. Create a new multi-class Java program which implements a vending machine simulator which contains the following functionality: A) At program startup, the vending machine is loaded with a variety of products in a variety of packaging for example soda/tonic/Coke in bottles, peanuts in bags, juice in cartons, etc. Also included is the cost of each item. The program should be designed to easily load a different set of products easily (for example, from a file). Also at program startup,...

  • LABWORK Design and Implement a Vending Machine Simulator that has functions : Show Products : D...

    LABWORK Design and Implement a Vending Machine Simulator that has functions : Show Products : Display Name , Description and Price of the Products Insert Coin : Allows user to Add a USD coin into the machine allowed types are : Nickel(0.05) , Dime(0.1) , Quarter(0.25) and Dollar (1) Buy: If the sufficient amount of coins are thrown into the machine , machine should dispense the cold drink , and remove it from its product inventory. If insufficient amount is...

  • INPUT FILE FOR PROJECT Sinclair Community College 0.50 6 0.75 5 0.50 9 0.75 4 1.00...

    INPUT FILE FOR PROJECT Sinclair Community College 0.50 6 0.75 5 0.50 9 0.75 4 1.00 3 Wright State University 0.90 4 1.00 6 0.90 3 1.00 2 1.25 4 University of Dayton 1.00 3 1.25 5 1.00 2 1.25 6 2.00 4 The Ohio State University 0.80 5 0.90 4 0.80 7 0905 3 1.50 2 Clark State Community College 0.75 1 0.85 2 0.75 4 0.85 2 1.00 5 Ohio University 1.00 2 1.25 6 1.00 7 1.25...

  • In C++, using: juiceMachine.h, juiceMachineImp.cpp, main.pp as your file names The method sellProduct of the Juice...

    In C++, using: juiceMachine.h, juiceMachineImp.cpp, main.pp as your file names The method sellProduct of the Juice Machine programming example gives the user only two chances to enter enough money to buy the product. Rewrite the definition of the method sellProduct so that it keeps prompting the user to enter more money as long as the user has not entered enough money to buy the product. Also, write a program to test your method. Your program should produce the following output:...

  • Please help, I am stuck with this problem. Each morning, Max White stocks the drink case at Max's Beach Hut in Myrtle B...

    Please help, I am stuck with this problem. Each morning, Max White stocks the drink case at Max's Beach Hut in Myrtle Beach, South Carolina. The drink case has 110 linear feet of refrigerated drink space. Each linear foot can hold either seven 12-ounce cans or three 20-ounce bottles. (Click the icon to view the information on the cold drinks.) Max's Beach Hut can sell all the drinks stocked in the display case each morning. Read the requirements Requirement 1....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT