function called from class::member fucntion

How can I call variable function from array?

food and drink are inside the class Food::food(...)

1
2
3
4
5
  int num =0;
  const int CAP=10;
  Food *foods[CAP];

foods[num] = new foods (food, drink);

If I type:
1
2
curry coke
pasta tea

drink variable will switch to tea
1
2
3
4
5
6
7
cout << drink; //show tea

cout << foods[1] = {drink} // want to show coke, but error

cout << drink-1 // show address only

cout << *(drink-1) // error: invalid type argument 


How can I show back to "coke" in this function??
How can (cout) print "coke" in this function??
Last edited on
Could you post your Food class?

How can I call variable function from array?

I'm not sure what you mean by this. Do you want to call the corresponding function depending on the input? Please clarify.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Food
{
  public:
    Food(string food,sring drink);

  private:
    string my_food;

   string my_drink;
};

Food::Food(string food,sring drink)
{
  my_food=food;
  m_drink=drink;
}

int main()
{
string food;
string drink;

    foods[CAP] = new food (food, drink);
...}


if I type two cin >>drink; the drink variable switch to tea,
but I want to show the 'coke' instead?
Last edited on
 
Food(string food,sring drink);

Typo.

 
foods[CAP] = new food (food, drink);

Not sure why you're dynamically allocating memory when you have a static array size. You're missing the type specifier as well.

I think you want something like this
1
2
3
4
5
6
7
8
9
10
11
Food foods[CAP];

for( int i = 0; i < CAP; i++ ) {
    string food, drink;
    cout << "enter food: ";
    getline( cin, food );
    cout << "enter drink: ";
    getline( cin, drink );

    foods[i] = Food( food, drink );
}

I want to put array[0] to two variable [food and drink]
and array[1] to another variable [food and drink]

After I put food and drink I want to show for things: first food and drink and second food and drink?




I want to put array[0] to two variable [food and drink]
and array[1] to another variable [food and drink]

I'm not sure why you would want to do that.
Could you post all the code you have right now?
Topic archived. No new replies allowed.