-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.

  • 5
    "I have watched countless hours of youtube videos" - this is where you are going wrong; you need to read a good C++ textbook. –  Oct 05 '18 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?) – Rup Oct 05 '18 at 23:07
  • 1
    https://ideone.com/e8DvzS – melpomene Oct 05 '18 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. – giusti Oct 05 '18 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. – Rup Oct 05 '18 at 23:09
  • @melpomene that is the response I want but I have further processing of the data eg. I need to shuffle the deck this is why I declared it – DevonFrm4200 Oct 05 '18 at 23:13
  • I understand you are new to C++, but they are at least 10 syntax errors in this code. I get the idea you're trying to make, but you must read a good textbook - not YouTube videos - if you want to grasp the basic syntax. There's simply no better way to put it. – RealPawPaw Oct 05 '18 at 23:15
  • @Rup Im well aware of proper naming conventions this function will go in a separate file, it will only be used inside a class but I need to get the code to work first before I can worry about commits for others to see – DevonFrm4200 Oct 05 '18 at 23:15
  • That is not deck, those are cards. Deck is sequence of cards that can be shuffled and from what cards can be drawn. – Öö Tiib Oct 05 '18 at 23:16
  • As said in my previous comment, there are simply too many errors. However, I've tried to fix as many errors as possible. https://ideone.com/7HdiSJ – RealPawPaw Oct 05 '18 at 23:22
  • You almost never want to dynamically allocate a `std::string`. Most of the point of `string` is to perform memory management for you and by dynamically allocating one, you're taking those responsibilities back upon yourself. Feel free to return `string`s by value. Compilers are very, very good at eliminating the extra copies. – user4581301 Oct 05 '18 at 23:23
  • As for shuffling a deck, [take a look at `std::shuffle`](https://en.cppreference.com/w/cpp/algorithm/random_shuffle) – user4581301 Oct 05 '18 at 23:25
  • @YoungNateFrmTTDT The naming thing isn't about style. Your code literally cannot work if you try to give the same name to two variables and a function. – melpomene Oct 05 '18 at 23:26
  • @user4581301 Yes, I should've. I was rushing too much. Thanks for the advice - I've edited it. – RealPawPaw Oct 05 '18 at 23:32
  • No worries. That was informative in case you weren't aware that you can do that. Too many people aren't. – user4581301 Oct 05 '18 at 23:39

1 Answers1

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';
}
Galik
  • 47,303
  • 4
  • 80
  • 117
  • 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. – bolov Oct 06 '18 at 00:05