Accessing an array of structs from a class

In the function buy drink I want to associate a user choice (entered in main) with an item name in DrinkInfo drinks[].

I tried this:

 
cout << drinks[choice].name;


This is not working. What is the proper way to access this array?

Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

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

const int DRINKS = 5;

struct DrinkInfo
{
	string name;
	int numDrinks;
	double price;
};

class SodaMachine
{
	private:
		void inputMoney();
		void dailyReport();	
	public:
		void buyDrink(int);
		void displayChoices();
		SodaMachine();
};

int main ()
{

	SodaMachine SodaMachine1;

	int choice = 0;

	do 
	{
		SodaMachine1.displayChoices();
		cin >> choice;

		SodaMachine1.buyDrink(choice);

	}while (choice != 6);

	return 0;
}

/***************SodaMachine::SodaMachine*****************/
//Class constructor defines the soda strcut array
SodaMachine::SodaMachine()
{
	DrinkInfo drinks[DRINKS] = {{"Cola", .75, 20}, {"Root Beer", .75, 20}, {"Orange Soda", .75, 20}, 
	                            {"Grape Soda", .75, 20}, {"Bottled Water", .75, 20}};
}

/*************SodaMachine::displayChoices************/
//This function displays a menu of drink choices
void SodaMachine::displayChoices()
{
	//outputs a menu
	cout << "*********************************\n";
	cout << "*    Drink Name            Cost *\n";
	cout << "*********************************\n";
	cout << " (1) Cola                  $0.75 \n";
	cout << " (2) Root Beer             $0.75 \n";
	cout << " (3) Orange Soda           $0.75 \n";
	cout << " (4) Grap Soda             $0.75 \n";
	cout << " (5) Bottled Water         $1.00 \n";
	cout << "_________________________________\n";
	cout << " \n (6) Quit                        \n";
	cout << endl;
	cout << "Please enter a selection: ";
}


/************SodaMachine::buyDrink***************/
//This function handles drink purchases
void SodaMachine::buyDrink(int choice)
{
	
}

/***********SodaMachine::inputMoney**************/
//This function takes the users money for the 
//drink purchase
void SodaMachine::inputMoney()
{


}

Last edited on
do you get any output or an error?
If I do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/***************SodaMachine::SodaMachine*****************/
//Class constructor defines the soda strcut array
SodaMachine::SodaMachine()
{
	DrinkInfo drinks[DRINKS] = {{"Cola", .75, 20}, {"Root Beer", .75, 20}, {"Orange Soda", .75, 20}, 
	                            {"Grape Soda", .75, 20}, {"Bottled Water", .75, 20}};
}

/*************SodaMachine::displayChoices************/
//This function displays a menu of drink choices
void SodaMachine::displayChoices()
{
	//outputs a menu
	cout << "*********************************\n";
	cout << "*    Drink Name            Cost *\n";
	cout << "*********************************\n";
	cout << " (1) Cola                  $0.75 \n";
	cout << " (2) Root Beer             $0.75 \n";
	cout << " (3) Orange Soda           $0.75 \n";
	cout << " (4) Grap Soda             $0.75 \n";
	cout << " (5) Bottled Water         $1.00 \n";
	cout << "_________________________________\n";
	cout << " \n (6) Quit                        \n";
	cout << endl;
	cout << "Please enter a selection: ";
}


/************SodaMachine::buyDrink***************/
//This function handles drink purchases
void SodaMachine::buyDrink(int choice)
{
    cout << "You picked " << drinks[choice].name << endl;
}


The error says:
cout << "You picked " << drinks[choice].name << endl;

indetifier drinks is undefined
Topic archived. No new replies allowed.