for each loop not working as expected

Feb 27, 2016 at 10:08pm
My goal is to print out object[] array using a for-each loop. My output is a string of addresses. I've tried subscripting but that defeats the purpose of a range-base for().

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
#include<iostream>
#include<array>

using namespace std;

struct Elements
{
	int x;
	int y;
};

int main()
{
	int object[52];

for (int x = 0; x<4; ++x)
{
	for (int y = 0; y<12; ++y)
	{
	object[y] = y;
	}
}
int ctr = 0;

for(auto &elem:object)
{
	std::cout<<object;
}
		return 0;
}



0x23fd200x23fd200x23fd200x23fd200x23fd200x23fd200x23fd200x23fd200x23fd200x23fd200x23fd200x23fd200x23fd200x23fd200x23...etc
Feb 27, 2016 at 10:26pm
std::cout << object;
The object is the name of an array. It decays to pointer and pointer prints its address.


PS. Your line 20. You update only the first 12 elements of object (but do it four times).
Feb 27, 2016 at 10:47pm
The loop should output elem, not object.

I misread the question at first, thought it was supposed to be an array of Elements which follows a similar style, but there is some added fun in getting it to work.
Feb 28, 2016 at 4:03pm
PS. Your line 20. You update only the first 12 elements of object (but do it four times).
-- fixed

Tried elem - get some garbage values. Also tried elem[], errors as well:

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
#include<iostream>
#include<array>

struct Elements
{
	int x;
	int y;
};

int main()
{
	int object[52];

	int ctr = 0;
		int card_array[52];
		for (int i = 0; i<4;++i)
		{
		for (int j = 0; j<12;++j)
		{
			ctr=ctr+1;
			card_array[ctr] = j;

		}
		}

ctr = 0;

for(auto &elem:object)
{
	std::cout<<elem;
}

ctr = 0;

		return 0;
}


01234567891011-4915232757004200376043143040-1-14198747023587040425865304257
Last edited on Feb 28, 2016 at 4:28pm
Feb 28, 2016 at 4:11pm
Your latest code is working. You might want to add some whitespace such as ' ' or '\t' or '\n' to the cout statement.
The garbage values are because the array is not properly initialised.

int object[52] = { 0 }; // set array to all zeros.


std::cout<<elem << ' '; // output a space between values

This is the output.
0 1 2 3 4 5 6 7 8 9 10 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0



Feb 28, 2016 at 7:25pm
It might be working but the code above is a mess. I corrected the array initialization, and the loop to create an array with 12 repeating elements x 4. I've been looking after the "elem" answer for quite a while. Now it makes sense. This also shows that for-each() loops can count through normal arrays. Though I wonder how far you can take it:instead of subscripted array could you also use a for-each loop with a pointer based array.

corrected 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
#include<iostream>
#include<array>

int main()
{

	int ctr(0);
		int objects[52] = {};
		for (int i = 0; i<4;++i)
		{

	for (int j = 1; j<14;++j)
		{
		objects[ctr] = j;
		std::cout<<objects[ctr];

		ctr=ctr+1;
		}

		}

ctr = 0;
std::cout<<std::endl;
for(auto &elem:objects)
{
	std::cout<<elem;
}

ctr = 0;

		return 0;
}

correct output:
12345678910111213123456789101112131234567891011121312345678910111213
Last edited on Feb 28, 2016 at 7:27pm
Topic archived. No new replies allowed.