Struct explanation

Hey, can someone explain this to me as I dont get the logic of it.

Is this a struct within a struct? and the last struct is accessing the global variables?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Drink
{
	Drink(string n, double cs, int cn)
	{
		name = n;
		cost = cs;
		count = cn;
	}
	Drink()
	{
		name = " ";
		cost = 0;
		count = 0;
	}

	string name;
	double cost;
	int count;
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.
O thanks for the information :)
hey again, im really struggling with struct

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>
using namespace 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 */
drink1, drink2, ... structs are initialized only with one string, for instance drink1's name is "Cola , 0.75, 20"

you want to initialize drinks with not only name, but with cost and number too - do it like
DrinkMachine drink1 = { "Cola", 0.75, 20 };

and don't forget to make an array of these structs
Last edited on
Is this correct?

1
2
3
4
5
DrinkMachine drink[5] = { { "Cola , 0.75, 20" },
	{ "Beer , 0.75, 20" },
	{ "Lemon-Lime , 0.75, 20" },
	{ "Grape Soda , 0.80, 20" },
	{ "Cream Soda , 0.80, 20" } };
kind of, except you are wrapping the whole thing in a string. you wrote "Cola, 0.75, 20" and you want to make it "Cola", 0.75, 20.
o wow, im so blind. I didnt even notice " ", think all this assignment work is killing me :(
meh

Last edited on
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. :-)
I coach people in C++ (particulary beginners), so if you'd like, email me at: sparkprogrammer@gmail.com

Joe
Last edited on
Topic archived. No new replies allowed.