How do I cout one element of a two dimensional array in main, which was initialized in a header file?

Just output one element?

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
 Deck.h
#pragma once
#include<iostream>
#include<string>

using namespace std;
class Deck
{
	
public:
	const char cardsList[4][13] = // initializer list of  all card letters, 4 rows (suits) by 13 columns(numbered cards)S
	{
	        { 'S','S','S' ,'S','S','S','S', 'S', '9', 'S', 'S', 'S', 'S' },
		{ 'S','S','S' ,'S','S','S','S', 'S', '9', 'S', 'S', 'S', 'S' },
		{ 'S','S','S' ,'S','S','S','S', 'S', '9', 'S', 'S', 'S', 'S' },
		{ 'S','S','S' ,'S','S','S','S', 'S', '9', 'S', 'S', 'S', 'S' }

	};


	int cardsListValued[4][13] // initializer list of all card values

	{

		{ 11,2,3,4,5,6,7,8,9,10,10,10,10 },//Spades
		{ 11,2,3,4,5,6,7,8,9,10,10,10,10 },//Clubs
		{ 11,2,3,4,5,6,7,8,9,10,10,10,10 },//Hearts
		{ 11,2,3,4,5,6,7,8,9,10,10,10,10 }//Diamonds

	};





};


1
2
3
4
5
6
7
8
9
10
11
12
13
14

Main.cpp
#include "Deck.h"

int main()
{
Deck myDeck;

cout <<  myDeck.cardsList[1][4]; // This outputs all characters up to 1,4. I only want it to output 1 character if the array

}


Last edited on
This outputs all characters up to 1,4.

I don't understand why it would do that. When I run your code it only outputs "S".
Since ..
initializer list of all card
consider making it a static data member and that can be accessed without any class object:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

struct Deck
{
    constexpr static int a2D [2][3] = {{1, 2, 3}, {4, 5, 6}};
};

int main()
{
    std::cout << Deck::a2D[0][0] << " ";
    std::cout << Deck::a2D[0][1] << " ";
    std::cout << Deck::a2D[0][2] << "\n";
    std::cout << Deck::a2D[1][0] << " ";
    std::cout << Deck::a2D[1][1] << " ";
    std::cout << Deck::a2D[1][2] << "\n";
}
/*Output
1 2 3
4 5 6
*/



Hello Beyond Humanity,

Not understanding your question or problem here. Line 9 of "main.cpp" only outputs one element of the array not up to as your comment says.

The cardsList[1][4] the 1 refers to row 1 which is actually row 2 since arrays start at 0. And 4 refers to to the 4th position which is actually element 5 or column 5.

Add this line to line 10 in "main": std::cout << " " << myDeck.cardsListValued[1][4]; and your output should be "S 5".

If that does not work for you try a different question.

Hope that helps,

Andy
Ok, accidently kept typing array[1,3] when it should be array[1][3]
Topic archived. No new replies allowed.