-1

Im new to c++, after trying to devlop in go I figured might as well learn c++ since the learning curve will be so steep the moment I start going down abstraction layers I'm use to handling dynamic arrays in php with ease however I cannot understand the pointers, and memory allocation, and scope issues associated with c++ heres my first attempt:

string* Deck() {
    // array of card rank and vlaue, seperated by a period, used as a delimiter for evaluting the players hands
    string* Deck = new string[52] ;
    Deck = {
        "A.H","2.H","3.H","4.H","5.H","6.H","7.H","8.H","9.H","10.H","J.H","Q.H","K.H",
        "A.S","2.S","3.S","4.S","5.S","6.S","7.S","8.S","9.S","10.S","J.S","Q.S","K.S",
        "A.D","2.D","3.D","4.D","5.D","6.D","7.D","8.D","9.D","10.D","J.D","Q.D","K.D",
        "A.C","2.C","3.C","4.C","5.C","6.C","7.C","8.C","9.C","10.C","J.C","Q.C","K.C",
    };

    return Deck;
}

int main()
{
    string* Deck = Deck();
    cout << *Deck[23] << endl;
    return 0;
}

the compiler is giving me various errors I've tried simply returning the pointers but I found out about the scope in this article Return string array in C++ function

Theres many articles regarding int as datatype but strings and int=>string explanations are rare as I guess everyone assumes even a noob could figure things out but I have watched countless hours of youtube videos and still can't quite figure it out . Thanks.

15
  • 5
    "I have watched countless hours of youtube videos" - this is where you are going wrong; you need to read a good C++ textbook. Commented Oct 5, 2018 at 23:05
  • "the compiler is giving me various errors" - can you post an error, and we can try and help you solve it? I've never worked with arrays of std::strings before but I'd guess that you just want cout << Deck[23] << endl; without the star: this would give cout a std::string which I think it can print without any further conversion. (I assume you didn't mean to dereference the pointer to get the first character only to print, as would have happened if this was an array of C not C++ strings?) Commented Oct 5, 2018 at 23:07
  • 1
    ideone.com/e8DvzS Commented Oct 5, 2018 at 23:08
  • There are just too many errors in this piece of code. People are not just being blunt with you. You really really need to get your hands on a book. You need to have a grasp of the language first; then you get to ask specific questions. Commented Oct 5, 2018 at 23:08
  • 1
    I'm also suspicious of calling everything Deck - particularly having both variables and a function called the same thing. Even if this works as-is (and I'm not sure it would) it would be clearer to a reader which Deck is which if you gave them different names. Commented Oct 5, 2018 at 23:09

1 Answer 1

4

" I'm use to handling dynamic arrays in php with ease however I cannot understand the pointers, and memory allocation, and scope issues associated with c++"

When dealing with dynamic arrays, you don't need to handle "pointers, and memory allocation, and scope issues" in C++ any more than you do in PHP.

Just use a std::vector, it manages the array for you:

std::vector<std::string> GetDeck() {

    // array of card rank and vlaue, seperated by a period, used as a delimiter for evaluting the players hands
    std::vector<std::string> Deck =
    {
        "A.H","2.H","3.H","4.H","5.H","6.H","7.H","8.H","9.H","10.H","J.H","Q.H","K.H",
        "A.S","2.S","3.S","4.S","5.S","6.S","7.S","8.S","9.S","10.S","J.S","Q.S","K.S",
        "A.D","2.D","3.D","4.D","5.D","6.D","7.D","8.D","9.D","10.D","J.D","Q.D","K.D",
        "A.C","2.C","3.C","4.C","5.C","6.C","7.C","8.C","9.C","10.C","J.C","Q.C","K.C",
    };

    return Deck;
}

int main()
{
    auto Deck = GetDeck();
    std::cout << Deck[23] << '\n';
}
Sign up to request clarification or add additional context in comments.

1 Comment

I second this. If you see any tutorial encouraging you to use raw pointers or god forbid manual memory management (raw new \ delete) know that's not how C++ should be used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.