Issues accessing values in std::array type C++












1















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;
}









share|improve this question


















  • 1





    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













  • 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















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;
}









share|improve this question


















  • 1





    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













  • 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








1








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;
}









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 19:34









AlanlyyyAlanlyyy

91




91








  • 1





    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













  • 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





    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













  • 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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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.






    share|improve this answer




























      1














      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.






      share|improve this answer


























        1












        1








        1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 19:42









        L.C.L.C.

        19012




        19012






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks