Some newbie help!

Why won't this
 
cout << card[1].name <<"\n"<<card[1].value<<endl;


hjärter 1
1

output this?

Sorry, I am new to programming and the sulotion is probably very obvious
but i would appreciate some help :)





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
139
140
141
142
143
144
145
146
147
#include "stdafx.h"
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

//Declaring functions and variables

int rndNr;
void loadDeck();

struct card{
	string name;
    int value;
	bool locked;
}card[52];

int main(){
	setlocale(LC_ALL,"swedish");

	//srand (unsigned int(NULL));
    void loadDeck();
	cout << card[1].name <<"\n"<<card[1].value<<endl;
	cin.get();
	return 0;
}


void loadDeck(){

//Hjärter

	card[1].name="Hjärter 2";
	card[1].value=1;
	card[2].name="Hjärter 3";
	card[2].value=2;
	card[3].name="Hjärter 4";
	card[3].value=3;
	card[4].name="Hjärter 5";
	card[4].value=4;
	card[5].name="Hjärter 6";
	card[5].value=5;
	card[6].name="Hjärter 7";
	card[6].value=6;
	card[7].name="Hjärter 8";
    card[7].value=7;
	card[8].name="Hjärter 9";
	card[8].value=8;
	card[9].name="Hjärter 10";
	card[9].value=9;
	card[10].name="Hjärter Knäkt";
	card[10].value=10;
	card[11].name="Hjärter Dam";
	card[11].value=11;
	card[12].name="Hjärter Kung";
	card[12].value=12;
	card[13].name="Hjärter Ess";
	card[13].value=13;

//Ruter
    card[14].name="ruter 2";
	card[14].value=1;
	card[15].name="ruter 3";
	card[15].value=2;
	card[16].name="ruter 4";
	card[16].value=3;
	card[17].name="ruter 5";
	card[17].value=4;
	card[18].name="ruter 6";
	card[18].value=5;
	card[19].name="ruter 7";
	card[19].value=6;
	card[20].name="ruter 8";
    card[20].value=7;
	card[21].name="ruter 9";
	card[21].value=8;
    card[22].name="ruter 10";
	card[22].value=9;
	card[23].name="ruter Knäkt";
	card[23].value=10;
	card[24].name="ruter Dam";
	card[24].value=11;
	card[25].name="ruter Kung";
	card[25].value=12;
	card[26].name="ruter Ess";
	card[26].value=13;

//spader

    card[27].name="Spader 2";
	card[27].value=1;
	card[28].name="Spader 3";
	card[28].value=2;
	card[29].name="Spader 4";
	card[29].value=3;
	card[30].name="Spader 5";
	card[30].value=4;
	card[31].name="Spader 6";
	card[31].value=5;
	card[32].name="Spader 7";
	card[32].value=6;
	card[33].name="Spader 8";
    card[33].value=7;
	card[34].name="Spader 9";
	card[34].value=8;
	card[35].name="Spader 10";
	card[35].value=9;
	card[36].name="Spader Knäkt";
	card[36].value=10;
	card[37].name="Spader Dam";
	card[37].value=11;
	card[38].name="Spader Kung";
	card[38].value=12;
	card[39].name="Spader Ess";
	card[39].value=13;

//klöver

    card[40].name="Hjärter 2";
	card[40].value=1;
	card[41].name="Hjärter 3";
	card[41].value=2;
	card[42].name="Hjärter 4";
	card[42].value=3;
	card[43].name="Hjärter 5";
	card[43].value=4;
	card[44].name="Hjärter 6";
	card[44].value=5;
    card[45].name="Hjärter 7";
	card[45].value=6;
	card[46].name="Hjärter 8";
    card[46].value=7;
	card[47].name="Hjärter 9";
	card[47].value=8;
	card[48].name="Hjärter 10";
	card[48].value=9;
	card[49].name="Hjärter Knäkt";
	card[49].value=10;
	card[50].name="Hjärter Dam";
	card[50].value=11;
	card[51].name="Hjärter Kung";
	card[51].value=12;
	card[52].name="Hjärter Ess";
	card[52].value=13;
}

Last edited on
Hi, i'm a newbie too, but ... what does it print out though ?

... and maybe the Index is zero-based (?). Why not declaring card[0] ? And why do you declare card[52] ?

I'm not sure about all that, because "card" is global, or ?

Maybe declare it inside the main function and let "loaddeck" manipulate it (call by reference) ... ?
That's what i would do, but i'm a newbie too, so ... no warranity ...

And put away the "void" before the call to "loaddeck", it's only needed in declarations, and your "call" to "loaddeck" maybe IS a declaration like this (? also not sure, but it's worth trying, i guess)
Last edited on
What is your question? if you want to display the cards use a for loop, also a tip, don't use global variables they could lead to confusion.
Last edited on
As fluppe says, you don't actually call loadDeck() in main() so the program doesn't load the card names and values into the array.

1
2
3
4
5
6
7
8
9
int main(){
	setlocale(LC_ALL,"swedish");

	//srand (unsigned int(NULL));
        loadDeck(); /// get rid of the void here.
	cout << card[1].name <<"\n"<<card[1].value<<endl;
	cin.get();
	return 0;
}


Now once you do this the program will enter your loadDeck() function but you will get maybe a crash.

Again as fluppe said, arrays start at index 0, i.e. card[0].
This means the last index of an array of 52 elements is 51, i.e. card[51].

With card[52] you are trying to access memory that is not part of the array (out of bounds).

To stop this you can redo the loadDeck() function to start at card[0] which is a lot of work or just make the array 53 elements long and never use card[0].

That is,

1
2
3
4
5
struct card{
	string name;
    int value;
	bool locked;
}card[53];/// change to 53 
Last edited on
Oooh yeaa, thanks guys!

This is basiclly blackjack I am trying to create, sorry for the confusion!
Topic archived. No new replies allowed.