can print std::Array from main, but not from function

This print function code works in main:
1
2
3
4
5
6
7
 while(ctr++ <52)
  {
	
 	cout<<card_deck[ctr].rank<<"   ";
	cout<<card_deck[ctr].suit<<endl;
  }
	return 0;


trying same in function:
1
2
3
4
5
6
7
8
9
10
11
12
void print_deck(array<Card_Deck, 52>&card_deck, int max_rank)
{
	int ctr(0);
	cout<<max_rank;
	while(ctr++ <max_rank)
	{
		cout<<ctr;
		cout<<"here";
		cout<<card_deck[ctr].rank<<endl;
		cout<<card_deck[ctr].suit<<endl;
	}
}


No error code.

Output:

ctr increments to 52
fine
zeros for array members
0-->cout<<card_deck[ctr].rank<<endl;
0-->cout<<card_deck[ctr].suit<<endl;
}
Why are you looping like that? std::array keeps track of it's own size so make a for-loop.
1
2
3
4
for(int i = 0; i < card_deck.size(); i++)
{
    // code
}


I don't understand the output part. Does the program output "ctr increments to 52" and then outputs "fine", because I dont see that anywhere in your code.
Last edited on
What I am having trouble with is passing the (array<Card_Deck, 52>&card_deck, int max_rank) into a function as opposed to running it in main (which works fine there). That's all.

I'll try to exploit more of the std::array features-- thx for that heads up.

In the meantime I need to be able to pass the array via the function.

Does the program output "ctr increments to 52" and then outputs "fine"

Not output, my commentary on the output. The ctr (ctr) works as predicted to 52. But array members .rank and .suit end up with 0 in the function. In main, by comparison, .rank and .suit are the correct result.


Thx
Last edited on
You should to post the (minimal) code to reproduce your supposed problem.
Otherwise, we can't imagine how you used the array and the objects contained into.
You should to post the (minimal) code to reproduce your supposed problem.
Otherwise, we can't imagine how you used the array and the objects contained into.


I believe that was in the first post. I'll post again. I believe everything is there to solve the problem.

This one works in main, no problems:
MAIN
1
2
3
4
5
6
7
while(ctr++ <52)
  {
	
 	cout<<card_deck[ctr].rank<<"   ";
	cout<<card_deck[ctr].suit<<endl;
  }
	return 0;


Now I want to compartmentalize and put the above in a function.
FX
1
2
3
4
5
6
7
8
9
10
11
12
void print_deck(array<Card_Deck, 52>&card_deck, int max_rank)
{
	int ctr(0);
	cout<<max_rank;
	while(ctr++ <max_rank)
	{
		cout<<ctr;
		cout<<"here";
		cout<<card_deck[ctr].rank<<endl;
		cout<<card_deck[ctr].suit<<endl;
	}
}


In the funck .rank and .suit are = 0. In main they are correct values.
Last edited on
I believe that was in the first post. I'll post again. I believe everything is there to solve the problem.

No that is not enough content for us to actually solve the problem.

First it doesn't show how you initialized the array, nor does it show how you called the function. For all we know you're print function in main() is using a different array than the array you passed into your function.

Also I don't see how you can say the code in main worked correctly since it doesn't print the first card, unless, of course, you forgot to post some relevant code that prints the first card. And it appears that you are probably accessing your array out of bounds in that loop as well.


Really you would be better off using a for() loop to print the array. Something like:
1
2
3
int ctr = 1;
for(auto& itr : card_deck)
    std::cout << ctr++ << " " << itr.rank << itr << " " << suit << std::endl; 


For us to actually see the problem you really need to post the smallest possible complete program that illustrates the problem.

Also I don't see how you can say the code in main worked correctly since it doesn't print the first card, unless, of course, you forgot to post some relevant code that prints the first card.

I am confused now. I thought

1
2
3
4
5
6
7
8
while(ctr++ <52)
  {
	
 	cout<<card_deck[ctr].rank<<"   ";
	cout<<card_deck[ctr].suit<<endl;
  }

	return 0;


prints the cards in main - at least that is what it does.
Is "ctr" initialized to 0? I would assume it is, if it is then during the first increment it will become 1. Meaning you will miss the first element of the array.
You'r correct. Would a do..while or std::array fix this one-off?
I'd suggest you do what @jlb suggested and use a range-based for-loop or the way I suggested with just a regular for-loop. If you still insist of using your while-loop, simply initialize ctr to -1.

Edit:

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

using namespace std;

void printArray(std::array<int, 10> & arr)
{
	for (int i = 0; i < arr.size(); i++)
	{
		cout << arr[i] << endl;
	}
}

int main(){
	
	std::array<int, 10> arr;
	
	for (int i = 0; i < arr.size(); i++)
	{
		arr[i] = i;
	}

	printArray(arr);

	return 0;
}


As you can see this works perfectly fine, so there must be something else that is wrong in your code, something you're not showing us.
Last edited on
Ok, I'm going to do what jlb suggested, that is if I can compensate for a one-off with for().
If you still insist of using your while-loop, simply initialize ctr to -1.

No, initialize ctr to zero, but increment the variable in the loop body, right before the end of the loop. Edit: Otherwise you'll access the array out of bounds on the last iteration of the loop. \Edit. But realize that this really why there is a for() loop.

1
2
3
4
5
6
7
8
   size_t ctr = 0;
   while(ctr < 52)
  {
	
 	cout << card_deck[ctr].rank << "   ";
	cout << card_deck[ctr].suit << endl;
        ctr++;
  }


And I don't recommend combining the increment with the last use of the counter, make it stand out by putting it on a line by it's self.

And really that "magic number" 52 should be a named constant.

const int DECK_SIZE = 52;

Last edited on
@technologist:

Now that you have discussed why the in-main loop works or not and @TarikNeaj has given you an example solution that works with a different function, we still have to address why we were not able to provide help initially.

In your first post you provided a loop and a function--that's all. You were asked to provide
the (minimal) code to reproduce your supposed problem. Otherwise, we can't imagine how you used the array and the objects contained int


Notice how @TarikNeaj posted code. This code contains an entire program. You can copy and paste into an IDE and compile the program to see what it does. We can read how the array is populated, and we can see how the function is called. If we were told there was a problem printing the array and all we had were lines 6-12, we would not be able to debug the problem.

When asked to provide the code required to reproduce the problem, please provide the data being worked on and the call to the function. A minimal standalone program (if possible) is best.
Ok, I'm going to do what jlb suggested, that is if I can compensate for a one-off with for().

What do you mean by "compensate for a one-off with for()"?

If you use the for() loop correctly there is nothing to compensate for.



Last edited on
Awesome - a further reason to use it. Thx

ps I've got to hone my approach to posting problems on the forum. I thought initally I provided enough information, but apparently not. I'll also try working on providing minimalist code. Now that I've learned what that means.

This code contains an entire program. You can copy and paste into an IDE and compile the program to see what it does. We can read how the array is populated, and we can see how the function is called.

I am always cautious about putting my entire code up, for concern of taking too much time from others.

Per Tarik he posted an example of passing an std::array through a function. Exactly what I was needing to do. The recommendations on loops were helpful as well.

Thx
Last edited on
I am always cautious about putting my entire code up, for concern of taking too much time from others.

That's why we say "minimal". There's no need to post everything. Just post something which is enough for us to reproduce the problem - which, obviously, there was no way we could do from your OP.

Per Tarik he posted an example of passing an std::array through a function.

So - does that mean that the problem was that you weren't passing in your array? Has TarikNeaj's example code shown you how to fix the problem?
So - does that mean that the problem was that you weren't passing in your array?


Yes

Has TarikNeaj's example code shown you how to fix the problem?


Yes
Which illustrates the point perfectly, because you didn't show us the code that actually had the problem in it. It was only pure luck that someone else happened to feel motivated to post an example, and that it happened to show you what it was doing wrong.
Topic archived. No new replies allowed.