Thread: returning a string array:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    returning a string array:

    I am trying to return a string array but am only getting the first char. I want to return something like this:

    8C TS KC 9H 4S

    so I can do my proper checks.

    Code:
    void PokerFile::splitHands(std::string playerHand)
    {
        for (int i=0; i<=13; i++)
        {
            playerOne[i]=playerHand[i];
        }
        
        for (int j=14; j<30; j++)
        {
            playerTwo[j]=playerHand[j];
        }
        checker();
    }
    
    std::string* PokerFile::getPlayerOne()
    {
        return playerOne;
    }
    
    std::string* PokerFile::getPlayerTwo()
    {
        return playerTwo;
    }
    
    void PokerFile::checker()
    {
        std::string check = *getPlayerOne();
        
        std::cout<<check<<" ";
        
    }
    
    //output
    8 5 3 T 7 5 6 T 7 J A 2 Q K 3 8 3 6 2 3 5 6 A 5 6 5 K 3 Q 2 7 3 A 2 5 K 9 9 2 K Q Q 3 2 4 4 8 5 5 4 Q K A J 8 K 8 8 Q 9 3 6 4 A 8 2 Q 7 Q J 2 K 4 8 A J 6 J 8 8 5 A K 9 A J K 9 7 A K 8 J J 6 4 6 2 8 5 J 4 T 7 8 K 3 9 5 8 T 5 5 6 A A K 4 Q 9 K 6 9 T 4 2 T 5 J A 9 J 2 9 6 K Q 2 T 9 2 T 5 K T 4 2 A 2 7 6 3 6 T 5 3 2 Q 8 2 K T K 6 T 4 J 4 T 6 3 4 4 4 8 Q T 9 4 9 4 A 5 3 3 2 5 7 8 Q 6 A 9 Q 5 6 A 4 9 2 3 J 7 J 7 4 6 2 7 4
    
    //file format: 1000 strings
    8C TS KC 9H 4S 7D 2S 5D 3S AC
    5C AD 5D AC 9C 7C 5H 8D TD KS
    3H 7H 6S KC JS QH TD JC 2D 8S
    TH 8H 5C QS TC 9H 4D JC KS JS
    7C 5H KC QH JD AS KH 4C AD 4S
    5H KS 9C 7D 9H 8D 3S 5D 5C AH
    6H 4H 5C 3H 2H 3S QH 5S 6S AS
    TD 8C 4H 7C TC KC 4C 3H 7S KS
    7C 9C 6D KD 3H 4C QS QC AC KH
    JC 6S 5H 2H 2D KD 9D 7C AS JS

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How is this different from String array and spaces:?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A pointer is not an array. Returning a pointer is not usually a safe or effective method of returning an array. Look up the standard containers (such as std::vector) for safer and more effective ways to return a collection of objects.

    Apart from that, the problem is not in the code you've shown. It would be in code that is populating playerOne. I'm not a mindreader so have no idea how you are populating playerOne, but the output is the first character of every line of input, so you are probably adding a new entry to the array (or whatever playerOne is) every time you read a character from the file, rather than when you read a line. That might indicate that, somewhere in the code that populates playerOne, your code is dereferencing something it shouldn't (for example, double-dereferencing a pointer to pointer, rather than only dereferencing once).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Arrays (or more correctly, C arrays) do not behave as you expect them to do. You can't return an array. In other situations, it returns a pointer to its first element. It's a mess, to say the least. Standard containers (std::vector for a dynamic array, std::array for a fixed size array) do not have this problem. They work just like regular variables - no exceptions. You should also learn about references, especially when dealing with arrays because it can speed up your program by a good amount.

    Of course, none of this helps your actual problem. But that can be discussed in the other thread.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Use function parameters to access arrays in other functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  2. Replies: 10
    Last Post: 12-04-2010, 12:04 AM
  3. Returning A String Array
    By Dajre in forum C Programming
    Replies: 10
    Last Post: 11-23-2010, 07:36 PM
  4. returning a string array from a function
    By steve1_rm in forum C Programming
    Replies: 15
    Last Post: 05-07-2008, 09:14 AM
  5. Replies: 9
    Last Post: 09-19-2007, 02:26 AM