Issues accessing values in std::array type C++
I am new to C++ and currently trying to write a simple Blackjack game. I am trying to create a Deck function which generates the 52 cards in an array. I am having issues with accessing the values in the std::array from my function.
I am calling the function "create_deck" in the main function, but I am only getting the first index of my Deck array. I think I am getting mixed up with pointers, but I am not sure how to debug this.
Any suggestions?
array<string,52> create_deck()
{
array<string,52> Deck; //array to store the deck
array<string,13> Cards = {"A","2","3","4",
"5","6","7","8",
"9","10","J","Q","K"}; //stores all the different card
int count = 0; //stores the index for Deck array
//index each card type
for(int card_index = 0; card_index < 13; card_index++)
{
//index each suit per card type
for(int suit_index = 0; suit_index < 4; suit_index++)
{
//append each suit of the specific card type to the deck
Deck[count] = Cards[card_index];
//check to see if cards are being added to the deck
cout << Deck.at(count) << endl;
}
}
return Deck;
}
int main() {
array<string,52> myDeck = create_deck();
for(int index =0; index<myDeck.size();index++)
{
cout << myDeck.at(index) <<endl;
}
return 0;
}
c++ stdarray
add a comment |
I am new to C++ and currently trying to write a simple Blackjack game. I am trying to create a Deck function which generates the 52 cards in an array. I am having issues with accessing the values in the std::array from my function.
I am calling the function "create_deck" in the main function, but I am only getting the first index of my Deck array. I think I am getting mixed up with pointers, but I am not sure how to debug this.
Any suggestions?
array<string,52> create_deck()
{
array<string,52> Deck; //array to store the deck
array<string,13> Cards = {"A","2","3","4",
"5","6","7","8",
"9","10","J","Q","K"}; //stores all the different card
int count = 0; //stores the index for Deck array
//index each card type
for(int card_index = 0; card_index < 13; card_index++)
{
//index each suit per card type
for(int suit_index = 0; suit_index < 4; suit_index++)
{
//append each suit of the specific card type to the deck
Deck[count] = Cards[card_index];
//check to see if cards are being added to the deck
cout << Deck.at(count) << endl;
}
}
return Deck;
}
int main() {
array<string,52> myDeck = create_deck();
for(int index =0; index<myDeck.size();index++)
{
cout << myDeck.at(index) <<endl;
}
return 0;
}
c++ stdarray
1
You are not incrementingcount
– Joseph Wood
Nov 24 '18 at 19:40
You can incrementcount
or you just writesuit_index * 13 + card_index
. You don't needcount
. A second problem I see is, you have no way to differ between apeak A
and aheart A
in the resulting array.
– Darkproduct
Nov 24 '18 at 19:49
Thank you, I really appreciate it! My code works fine now.
– Alanlyyy
Nov 24 '18 at 19:55
I am currently writing a basic black jack game, the suits will be incorporated later. Thank you!
– Alanlyyy
Nov 25 '18 at 18:42
Very intresting read on how to debug a small program: ericlippert.com/2014/03/05/how-to-debug-small-programs
– L.C.
Nov 26 '18 at 13:06
add a comment |
I am new to C++ and currently trying to write a simple Blackjack game. I am trying to create a Deck function which generates the 52 cards in an array. I am having issues with accessing the values in the std::array from my function.
I am calling the function "create_deck" in the main function, but I am only getting the first index of my Deck array. I think I am getting mixed up with pointers, but I am not sure how to debug this.
Any suggestions?
array<string,52> create_deck()
{
array<string,52> Deck; //array to store the deck
array<string,13> Cards = {"A","2","3","4",
"5","6","7","8",
"9","10","J","Q","K"}; //stores all the different card
int count = 0; //stores the index for Deck array
//index each card type
for(int card_index = 0; card_index < 13; card_index++)
{
//index each suit per card type
for(int suit_index = 0; suit_index < 4; suit_index++)
{
//append each suit of the specific card type to the deck
Deck[count] = Cards[card_index];
//check to see if cards are being added to the deck
cout << Deck.at(count) << endl;
}
}
return Deck;
}
int main() {
array<string,52> myDeck = create_deck();
for(int index =0; index<myDeck.size();index++)
{
cout << myDeck.at(index) <<endl;
}
return 0;
}
c++ stdarray
I am new to C++ and currently trying to write a simple Blackjack game. I am trying to create a Deck function which generates the 52 cards in an array. I am having issues with accessing the values in the std::array from my function.
I am calling the function "create_deck" in the main function, but I am only getting the first index of my Deck array. I think I am getting mixed up with pointers, but I am not sure how to debug this.
Any suggestions?
array<string,52> create_deck()
{
array<string,52> Deck; //array to store the deck
array<string,13> Cards = {"A","2","3","4",
"5","6","7","8",
"9","10","J","Q","K"}; //stores all the different card
int count = 0; //stores the index for Deck array
//index each card type
for(int card_index = 0; card_index < 13; card_index++)
{
//index each suit per card type
for(int suit_index = 0; suit_index < 4; suit_index++)
{
//append each suit of the specific card type to the deck
Deck[count] = Cards[card_index];
//check to see if cards are being added to the deck
cout << Deck.at(count) << endl;
}
}
return Deck;
}
int main() {
array<string,52> myDeck = create_deck();
for(int index =0; index<myDeck.size();index++)
{
cout << myDeck.at(index) <<endl;
}
return 0;
}
c++ stdarray
c++ stdarray
asked Nov 24 '18 at 19:34
AlanlyyyAlanlyyy
91
91
1
You are not incrementingcount
– Joseph Wood
Nov 24 '18 at 19:40
You can incrementcount
or you just writesuit_index * 13 + card_index
. You don't needcount
. A second problem I see is, you have no way to differ between apeak A
and aheart A
in the resulting array.
– Darkproduct
Nov 24 '18 at 19:49
Thank you, I really appreciate it! My code works fine now.
– Alanlyyy
Nov 24 '18 at 19:55
I am currently writing a basic black jack game, the suits will be incorporated later. Thank you!
– Alanlyyy
Nov 25 '18 at 18:42
Very intresting read on how to debug a small program: ericlippert.com/2014/03/05/how-to-debug-small-programs
– L.C.
Nov 26 '18 at 13:06
add a comment |
1
You are not incrementingcount
– Joseph Wood
Nov 24 '18 at 19:40
You can incrementcount
or you just writesuit_index * 13 + card_index
. You don't needcount
. A second problem I see is, you have no way to differ between apeak A
and aheart A
in the resulting array.
– Darkproduct
Nov 24 '18 at 19:49
Thank you, I really appreciate it! My code works fine now.
– Alanlyyy
Nov 24 '18 at 19:55
I am currently writing a basic black jack game, the suits will be incorporated later. Thank you!
– Alanlyyy
Nov 25 '18 at 18:42
Very intresting read on how to debug a small program: ericlippert.com/2014/03/05/how-to-debug-small-programs
– L.C.
Nov 26 '18 at 13:06
1
1
You are not incrementing
count
– Joseph Wood
Nov 24 '18 at 19:40
You are not incrementing
count
– Joseph Wood
Nov 24 '18 at 19:40
You can increment
count
or you just write suit_index * 13 + card_index
. You don't need count
. A second problem I see is, you have no way to differ between a peak A
and a heart A
in the resulting array.– Darkproduct
Nov 24 '18 at 19:49
You can increment
count
or you just write suit_index * 13 + card_index
. You don't need count
. A second problem I see is, you have no way to differ between a peak A
and a heart A
in the resulting array.– Darkproduct
Nov 24 '18 at 19:49
Thank you, I really appreciate it! My code works fine now.
– Alanlyyy
Nov 24 '18 at 19:55
Thank you, I really appreciate it! My code works fine now.
– Alanlyyy
Nov 24 '18 at 19:55
I am currently writing a basic black jack game, the suits will be incorporated later. Thank you!
– Alanlyyy
Nov 25 '18 at 18:42
I am currently writing a basic black jack game, the suits will be incorporated later. Thank you!
– Alanlyyy
Nov 25 '18 at 18:42
Very intresting read on how to debug a small program: ericlippert.com/2014/03/05/how-to-debug-small-programs
– L.C.
Nov 26 '18 at 13:06
Very intresting read on how to debug a small program: ericlippert.com/2014/03/05/how-to-debug-small-programs
– L.C.
Nov 26 '18 at 13:06
add a comment |
1 Answer
1
active
oldest
votes
There's a missing count++ at the end of the inner loop. You're not sure how to debug this: run it in debug mode and "step by step" checking the different variables could help.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461701%2fissues-accessing-values-in-stdarray-type-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There's a missing count++ at the end of the inner loop. You're not sure how to debug this: run it in debug mode and "step by step" checking the different variables could help.
add a comment |
There's a missing count++ at the end of the inner loop. You're not sure how to debug this: run it in debug mode and "step by step" checking the different variables could help.
add a comment |
There's a missing count++ at the end of the inner loop. You're not sure how to debug this: run it in debug mode and "step by step" checking the different variables could help.
There's a missing count++ at the end of the inner loop. You're not sure how to debug this: run it in debug mode and "step by step" checking the different variables could help.
answered Nov 24 '18 at 19:42
L.C.L.C.
19012
19012
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461701%2fissues-accessing-values-in-stdarray-type-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You are not incrementing
count
– Joseph Wood
Nov 24 '18 at 19:40
You can increment
count
or you just writesuit_index * 13 + card_index
. You don't needcount
. A second problem I see is, you have no way to differ between apeak A
and aheart A
in the resulting array.– Darkproduct
Nov 24 '18 at 19:49
Thank you, I really appreciate it! My code works fine now.
– Alanlyyy
Nov 24 '18 at 19:55
I am currently writing a basic black jack game, the suits will be incorporated later. Thank you!
– Alanlyyy
Nov 25 '18 at 18:42
Very intresting read on how to debug a small program: ericlippert.com/2014/03/05/how-to-debug-small-programs
– L.C.
Nov 26 '18 at 13:06