the information here is for c++/basic oop paradigms. this doesnt work with c. anyways...
it is possible to have nested structs (structs within structs) but that isnt what is happening here. so you have a data structure called Drink. the first two "functions" (called methods inside a class or struct) are called constructors. the first, Drink(string, double, int) works like this:
lets say you have declare a Drink variable. Drink myDrink. however, that wont work. instead you would need to make it look like this: Drink myDrink("soda", 10.0, 9001);, which gives it those values. as to the second, i lied a bit about Drink myDrink not working. it works because of the second constructor, which is referred to as the default constructor (since it takes no values). it lets you declare a variable like that.
So far I have created this, am I on the right track?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
struct DrinkMachine
{
string name; //name for drinks
double cost; //cost of drinks
int num; // number of drinks inside machine
};
int main()
{
DrinkMachine drink1 = { "Cola , 0.75, 20" };
DrinkMachine drink2 = { "Beer , 0.75, 20" };
DrinkMachine drink3 = { "Lemon-Lime , 0.75, 20" };
DrinkMachine drink4 = { "Grape Soda , 0.80, 20" };
DrinkMachine drink5 = { "Cream Soda , 0.80, 20" };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*Write a program that simulates a soft drink machine. The program should use a structure (struct) 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
(in pence)
Cola 0.75 20
Beer 0.75 20
Lemon-Lime 0.75 20
Grape Soda 0.80 20
Cream Soda 0.80 20 */
Hey all, im need some advice on what is the best way to program this.
Read the instructions?
Each time the program runs, it should enter a loop that performs the following steps: A list of drinks is
displayed on the screen.
The first thing you have is asking if the user wants to quit or see the menu. If they don't quit, they only have one opportunity to make a selection also. The easiest way to fix that is to use a do/while loop that starts with the menu and having quit as one one of the options.
Think about a real drink machine and what you see when you first walk up to it. It will show you what you have to select from and how much it costs, but it doesn't show you the quantity of each type. It only tells you it doesn't have it if you select it like the instructions say.
If the user selects a drink that has sold out, a message should be displayed.
Just think of the fun you'll have typing out a new menu for every possibility when the user makes a second selection, or a third. :-)