Question

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 stor

Drink Name Cost Number in Machine Cola .75 20 Root Beer .75 20 Lemon-Lime .75 20 Grape Soda .80 20 Cream Soda .80 20 Each tim

Each time the program runs, it should enter a loop that performs the following steps: A list of drinks is displayed on the sc

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

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

// Defines a structure to store drink inventory
struct DrinkMachine
{
// Data member to store drink information
string drinkName;
double cost;
int numberInMachine;
};// End of structure

// Function to display drink information
void showDrinks(DrinkMachine dm[], int len)
{

cout<<"\n\n ******************** Drinks Available ********************\n";
cout<<left<<setw(8)<<"Index"<<setw(20)<<"Drink Name"<<setw(10)<<"Cost"<<setw(20)<<"Number In Machine"<<endl;

// Loops till number of drinks available
for(int c = 0; c < len; c++)
// Displays the current drink
cout<<setw(8)<<(c)<<setw(20)<<dm[c].drinkName<<fixed<<setprecision(2)<<setw(10)<<dm[c].cost
<<setw(20)<<dm[c].numberInMachine<<endl;
}// End of function

// Function to return valid drink name entered by the user
// Returns the index position
int validDrink(DrinkMachine dm[], int len)
{
// To store the number entered by the user
int no;

// Loops till valid index entered by the user
do
{
// Accepts drink name index from the user
cout<<"\n Enter the drink index number: ";
cin>>no;

// Checks if the number is greater than or equals to 0 and less than length
if(no >= 0 && no < len)
// return the index
return no;

// Otherwise displays error message
else
cout<<"\n\t Invalid index. Try again.";
}while(1); // End of do - while loop
}// End of function

// Function to return valid cost entered by the user
double validCost(DrinkMachine dm[], int len, double productCost)
{
// To store the number entered by the user
double amount;

// Loops till valid amount entered by the user
do
{
// Accepts amount from the user
cout<<"\n Enter the amount: ";
cin>>amount;

// Checks if the amount is greater than or equals to 1.0
if(amount >= 1.0)
// Checks if the amount is greater than or equals to the purchased cost
if(amount >= productCost)
// Returns the amount
return amount;
else
cout<<"\n Insufficient amount. Product cost is: $"<<productCost;
// Otherwise displays error message
else
cout<<"\n\t Invalid cost. Try again.";
}while(1); // End of do - while loop
}// End of function

// main function definition
int main()
{
// Creates an array of object and assigns data
DrinkMachine dm[] =
{
{"Cola", 75.0, 20}, {"Root Beer", 75.0, 20}, {"Lemon Lime", 75.0, 20},
{"Grape Soda", 80.0, 20}, {"Cream", 80.0, 20}
};// End of initialization

// Calculates number of records
int len = sizeof(dm) / sizeof(dm[0]);

int choice;
int index;
double amount;

// Loops till user choice is not 2
do
{
// Calls the function to display drinks information
showDrinks(dm, len);

// Displays menu and accepts user choice
cout<<"\n 1 - Pick a drink. \n 2 - Exit. \n\t What is your choice? ";
cin>>choice;

// Checks if choice is 2 then stop the loop
if(choice == 2)
break;

// Otherwise checks if choice is 1
else if(choice == 1)
{
// Calls the function to accept valid drink
// Stores the return index
index = validDrink(dm, len);

// Checks if required drink quantity is not 0
if(dm[index].numberInMachine != 0)
{
// Calls the function to accept valid amount
amount = validCost(dm, len, dm[index].cost);

// Subtracts one quantity
dm[index].numberInMachine--;

// Checks if amount paid is greater than the product cost
if(amount > dm[index].cost)
// Calculates and displays change returned
cout<<"\n Take your change: $"<<(amount - dm[index].cost);
}// End of if condition

// Otherwise quantity is 0. Display error message
else
cout<<"\n Product is sold out.";
}// End of else if condition

// Otherwise invalid choice
else
cout<<"\n Invalid choice!!";

}while(1);// End of do - while loop
return 0;
}// End of main function

Sample Output:

Index 1 2 3 4 Drink Name Cola Root Beer Lemon Lime Grape Soda Cream Drinks Available Cost Number In Machine 75.00 20 75.00 20

Add a comment
Know the answer?
Add Answer to:
q13 c++ if answering please do not write code in paragrapth form. 13. Drink Machine Simulator...
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
  • Drink Machine Simulator C++

    Drink Machine SimulatorThe 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 NameDrink CostNumber of Drinks in MachineThe program should create an array of five structures. The elements should be initialized with the following data:Drink Name Cost Number in MachineCoca-Cola .75 20Root Beer .75 20Sprite .75 20Spring Water .80 20Apple Juice .80...

  • 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...

  • 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...

  • Just do program 6 using c++ which are printf and scanf. Please don’t use cout and...

    Just do program 6 using c++ which are printf and scanf. Please don’t use cout and cin. Program 4: A slot machine has three windows. A random fruit is picked for each window from cherry apple, lemon, and orange. If all three windows match, the user wins 8 times the bet amount. If the first two windows only are cherries, the user wins 3 times the bet amount. If the first window is a cherry and the second window is...

  • Please solve using c++ plus There are two text files with the following information stored in...

    Please solve using c++ plus There are two text files with the following information stored in them: The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma The department.txt file where each line stores the name, location and budget of the department separated by a comma You need to write a C++ program that reads these text files and provides user with the following menu: 1. Enter the instructor ID...

  • Write the code in Python. The output should be exact the same as the given.

    Write the code in Python. The output should be exact the same as the given. Question 1. Study the examples below carefully and write a program for journal subscription Your program should work exactly as the following examples The text in bold indicates user input. Example 1: The user is a student, and the user subscribes to 2 journals Question 1 - Journal of Commodity Markets Journal of Sustainable Mining Journal of Asia-Pacific Biodiversity Student $21 $22 $27 Non-student $50...

  • Programming question. Using Visual Studio 2019 C# Windows Form Application (.NET Framework) Please include code for...

    Programming question. Using Visual Studio 2019 C# Windows Form Application (.NET Framework) Please include code for program with ALL conditions met. Conditions to be met: Word Problem A person inherits a large amount of money. The person wants to invest it. He also has to withdraw it annually. How many years will it take him/her to spend all of the investment that earns at a 7% annual interest rate? Note that he/she needs to withdraw $40,000.00 a year. Also there...

  • This is done in c programming and i have the code for the programs that it wants at the bottom i ...

    This is done in c programming and i have the code for the programs that it wants at the bottom i jut dont know how to call the functions Program 2:Tip,Tax,Total int main(void) {    // Constant and Variable Declarations    double costTotal= 0;    double taxTotal = 0;    double totalBill = 0;    double tipPercent = 0;    // *** Your program goes here ***    printf("Enter amount of the bill: $");    scanf("%lf", &costTotal);    printf("\n");    // *** processing ***    taxTotal = 0.07 * costTotal;    totalBill...

  • 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field ...

    12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field .A String named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up The Coin class should have the following methods .A no-arg constructor that randomly determines the side of the coin that is facing up (heads" or "tails") and initializes the aideUp field accordingly .A void method named toss that simulates the...

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