Simple question about instances and for loops

Hello,

I've got 8 instances named food0,food1,food2 ... until food7. They're instances of a class which contains 3 ints.

When i want to cout the stunErbij int of ALL the food instances i do this:
1
2
3
4
for (int t=0;t<8;t++)
{
      cout>> food[t].stunErbij;
}


And it's completely wrong :).. But how do i do it right?

Do I need to put all the food instances in an aray?
Last edited on
Hi BMH,

firstly the cout statement is wrong, the syntax is:

 
cout << "some value here" << "some other value here";

So in your case it should be:
1
2
3
4
for (int t=0;t<8;t++)
{
      cout << food[t].stunErbij;
}

Also ensure that you the statement:
 
using namespace std;

before you start writing any code on your source file.
I think that should then work!
If it still doesn't work please post all your source code and the error that you get from the compiler.
Good luck,
Matt
Last edited on
Oops. Stupid mistake :).. It still doesn't work.. It's trying to acces a array i think. and not the instance name.
error: no match for 'operator[]' in 'food[t]'
Please could you post your source code, also including the class definition. That should make it easier to track down the problem :).
Thanks,
Matt
I'm creating a "field" in the console. The code needs to check if the X & Y values of ALL the food instances match the field.

This is the important part of the code:

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <cstdlib>
#include <ctime> 
#include <vector>
#include <string>
using namespace std;

//Produce classes
class FOOD    
{              
 public:
    int x;
    int y;
	int stunErbij;


    FOOD(int x, int y, int stunErbij)        // Init-constructor.
        {
        this->x = y;
        this->y = x;
		this->stunErbij = stunErbij;
        }
};

class APPENDIX
{
	public:
	int x;
	int y;
	int stun;
	
	APPENDIX(int x,int y, int stun)		// Init-constuctor
	{
		this->x = y;
		this->y = x;
		this->stun = stun;
	}
};

class PLAYER
{
	public:
	int x;
	int y;
	int punten;
	
	PLAYER(int x,int y, int punten)			// Init-constructor.
	{
		this->x = y;
		this->y = x;
		this->punten = punten;
	}
};


int main() // HEAD PROGRAM.
{
	int temporaryAppendixCoordinate[2]= {0,0}; 

	//Random number for spawning objects
//	const int	aantalfood = 8;
	const int   fieldsize = 15;
//	int randomSpawn[4];
	srand((unsigned)time(NULL)); 
	
	//String produced for controls
	string	wasd1234;
	
	//Iterator for producing random numbers
/*	for (int i=0; i<10; i++)
	{	
		srand((unsigned)time(NULL));
		int number = rand();
		randomSpawn[i] = (number % (fieldsize-1));
	}
*/
	
    //Instances
	PLAYER	player1(7,7,0);	//player 1 position
	APPENDIX  appendix1(temporaryAppendixCoordinate[0],temporaryAppendixCoordinate[1],0);
	FOOD     food0(9,9,3);  
	FOOD     food1(5,3,3);
	FOOD     food2(9,7,3);  
	FOOD     food3(4,8,3);  
	FOOD     food4(2,3,3);  
	FOOD     food5(1,5,3);  
	FOOD     food6(8,5,3);  
	FOOD     food7(4,5,3); 
	

		
// Infinite loop
 for(bool i=true;i==true;)
 {
		system("CLS");
		//Produce the field		
		for (int x=0; x<fieldsize; x++)
        {
			for (int y=0; y<fieldsize; y++)
            {
				if ((appendix1.x == x) && (appendix1.y == y))
					if (appendix1.stun > 0)
					{
						cout << ">" << appendix1.stun << "<";
					}
					else
					{
						cout << ">!<";
					}	
// Here the code needs to check ALL the food instances instead of just food0!!!				
					else if	((food0.x == x) && (food0.y == y))
					cout << "+" << food0.stunErbij << " ";
					else if ((player1.x == x) && (player1.y == y))
					cout << "€  ";
					else
					cout << ".  ";
            }
			cout << endl;
        }
}
    return 0;
}		
BUMP! This is the problem.
1
2
3
4
5
6
7
8
9
10
// Here the code needs to check ALL the food instances instead of just food0!!!				
					else if	((food0.x == x) && (food0.y == y))
					cout << "+" << food0.stunErbij << " ";
					else if ((player1.x == x) && (player1.y == y))
					cout << "€  ";
					else
					cout << ".  ";
            }
			cout << endl;
        }
@BMH:
You are mixing tab characters and space characters in the indentation of your code. That's why it's looking so messy. I recommend that you replace all tabs with four spaces, and set your editor to always to that.
Yes. Thank you. I'll do that next time.

I can't seem to figure this out :(.
Last edited on
Whoa. You really don't believe in asking simple questions, do you? This is far more complicated than it appears on the surface of things, because you're trying to get your logic to encompass a group of instances. This works:

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <cstdlib>
#include <ctime> 
#include <vector>
#include <string>
using namespace std;

//Produce classes
class FOOD
{
      public:
             int x;
             int y;
             int stunErbij;
             FOOD *ptr;
             FOOD(int x, int y, int stunErbij);
};

vector<FOOD*> ptrvector;

FOOD::FOOD(int x_input, int y_input, int stunErbij_input)
{
    x = x_input;
    y = y_input;
    stunErbij =stunErbij_input;
    ptr = this;
    ptrvector.push_back(ptr);
}

bool Compare(int x_input, int y_input, vector<FOOD*>)
{
     bool toggle = false;
     for(int fooditer = 0; fooditer < ptrvector.size(); fooditer++)
     {
        if(ptrvector[fooditer]->x == x_input && ptrvector[fooditer]->y ==y_input){toggle = true;}
        }
        return toggle;
}


class APPENDIX
{
	public:
	int x;
	int y;
	int stun;
	
	APPENDIX(int x,int y, int stun)		// Init-constuctor
	{
		this->x = y;
		this->y = x;
		this->stun = stun;
	}
};

class PLAYER
{
	public:
	int x;
	int y;
	int punten;
	
	PLAYER(int x,int y, int punten)			// Init-constructor.
	{
		this->x = y;
		this->y = x;
		this->punten = punten;
	}
};


int main() // HEAD PROGRAM.
{
	int temporaryAppendixCoordinate[2]= {0,0}; 

	//Random number for spawning objects
//	const int	aantalfood = 8;
	const int   fieldsize = 15;
//	int randomSpawn[4];
	srand((unsigned)time(NULL)); 
	
	//String produced for controls
	string	wasd1234;
	
	//Iterator for producing random numbers
/*	for (int i=0; i<10; i++)
	{	
		srand((unsigned)time(NULL));
		int number = rand();
		randomSpawn[i] = (number % (fieldsize-1));
	}
*/
	
    //Instances
	PLAYER	player1(7,7,0);	//player 1 position
	APPENDIX  appendix1(temporaryAppendixCoordinate[0],temporaryAppendixCoordinate[1],0);
	FOOD     food0(9,9,3);  
	FOOD     food1(5,3,3);
	FOOD     food2(9,7,3);  
	FOOD     food3(4,8,3);  
	FOOD     food4(2,3,3);  
	FOOD     food5(1,5,3);  
	FOOD     food6(8,5,3);  
	FOOD     food7(4,5,3); 
	

		
// Infinite loop
 for(bool i=true;i==true;)
 {
		system("CLS");
		//Produce the field		
		for (int x=0; x<fieldsize; x++)
        {
			for (int y=0; y<fieldsize; y++)
            {
				if ((appendix1.x == x) && (appendix1.y == y))
					if (appendix1.stun > 0)
					{
						cout << ">" << appendix1.stun << "<";
					}
					else
					{
						cout << ">!<";
					}	
// Here the code needs to check ALL the food instances instead of just food0!!!				
					else if	(Compare(x,y,ptrvector))
					cout << "+" << food0.stunErbij << " ";
					else if ((player1.x == x) && (player1.y == y))
					cout << "€  ";
					else
					cout << ".  ";
            }
			cout << endl;
        }
}
    return 0;
}


Given your reaction to the code I posted last time, this might be a little over your head, but here's an explanation:

Within the FOOD class I've declared a pointer (FOOD *ptr) that during the call to the constructor is assigned to point to the instance created whenever a new FOOD is made. That pointer is then stored in a vector of pointers to type FOOD. (If you're never planning on using more than a certain maximum number of instances of FOOD you could always just make it an array of pointers. I just used a vector so that you can create as many instances of FOOD as you want.) I then defined the Compare function which takes the x and y coordinates passed to it, and compares them to the x and y coordinates of each instance of FOOD, and returns a bool of true if the coordinates match, and false if they don't. That function is then used in the loop used to create your field.

If you have any questions about how any specific portion of the code I added works, just let me know, and I'll be happy to answer back.

By the way: What is the stunErbij variable for?
You are a saint!

stunErbij is the number of "turns" the enemy is "stunned".

Got 1 more "simple question. How do i do the same thing outside of this for loop? Like say i want to detect if the player collides with ANY of the food instances.
1
2
3
4
5
//Collision detection with food. 
if ((player1.x == food0.x) && (player1.y == food0.y))
	{
                food0.x = -1;
        }	
a
Last edited on
Anyone?
I've got about.... 11 hours before my deadline :).
Topic archived. No new replies allowed.