Function not returning structure with proper data

I'm trying to write a function which finds the lowest card out of 2 player's hands. However whenever I run the program it returns an empty.

I set break points and its only setting playerWithLowCard and not the card Template.

My Class and Structure:
1
2
3
4
5
6
7
8
9
10
11
12
13
class CardTemplate
{
public:
    std::string suit;
    int suitStrength;
    int cardNum;
};

struct lowCardStruct
{
    int playerWithLowCard;
    CardTemplate lowestCard;
};


My function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  lowCardStruct determineLowestCard(CardTemplate pOneLowest, CardTemplate pTwoLowest)
{
    //create structure to store who has lowest card and which it is
    lowCardStruct lowCardInPlay;
    if(pOneLowest.cardNum < pTwoLowest.cardNum && pOneLowest.suitStrength <= pTwoLowest.suitStrength)
    {
        std::cout << "ONE" << std::endl;
        lowCardInPlay.playerWithLowCard = 0;
        lowCardInPlay.lowestCard = pOneLowest;
        return lowCardInPlay;
    }
    else
    {
        std::cout << "TWO" << std::endl;
        lowCardInPlay.playerWithLowCard = 1;
        lowCardInPlay.lowestCard = pTwoLowest;
        return lowCardInPlay;
    }
}


Check in main:
1
2
std::cout << "Player: " << (lowestCardInPlay.playerWithLowCard + 1) << std::endl;
    std::cout << "Their lowest card is: " << lowestCardInPlay.lowestCard.suit << " : " << lowestCardInPlay.lowestCard.cardNum << std::endl;
- overload operator < for CardTemplate to take into a/c all possibilites
- sort each player/hand and compare lowest card of all the players to get the player who has lowest card of all
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <random>
#include <chrono>

enum Suit {Spades, Hearts, Clubs, Diamonds};//change as required;
std::ostream& operator << (std::ostream& os, const Suit& s)//for printing Suit objects
{
    switch(s)
    {
        case Spades: return os << "Spades";
        case Hearts: return os << "Hearts";
        case Clubs: return os << "Clubs";
        case Diamonds: return os << "Diamonds";
        default : return os << "";
    }
}
const std::string RANK[]  = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

struct Card
{
    Suit m_itsSuit;
    std::string m_itsRank;
    Card(const Suit& itsSuit, const std::string& itsRank) :
        m_itsSuit(itsSuit), m_itsRank(itsRank){};
};
bool operator < (const Card& lhs, const Card& rhs)//for sorting Card objects
{
    return lhs.m_itsSuit < rhs.m_itsSuit ||
        (!(rhs.m_itsSuit < lhs.m_itsSuit) && lhs.m_itsRank < rhs.m_itsRank);
}
std::ostream& operator << (std::ostream& os, const Card& c)//for printing Card objects
{
    os << c.m_itsSuit << " " << c.m_itsRank << "\n";
    return os;
}

int main()
{
    std::default_random_engine numGenerator(static_cast<unsigned int>(time(NULL)));
    std::uniform_int_distribution<int> rangeSuit(0, 3);
    std::uniform_int_distribution<int> rangeRank(0, 12);

    std::vector<Card> playerA{};
    std::vector<Card> playerB{};

    for (size_t i = 0; i < 5; ++i)
    {
        //create 2 hands of random cards
        playerA.emplace_back(Card(static_cast<Suit>(rangeSuit(numGenerator)), RANK[rangeRank(numGenerator)]));
        playerB.emplace_back(Card(static_cast<Suit>(rangeSuit(numGenerator)), RANK[rangeRank(numGenerator)]));
    }
    //sort the hands
    std::sort(playerA.begin(), playerA.end());
    std::sort(playerB.begin(), playerB.end());

    //print the hands
    std::cout << "Player A\n";
    for (const auto& elem : playerA)std::cout << elem ;
    std::cout << "\nPlayer B\n";
    for (const auto& elem : playerB)std::cout << elem;

    //check which hand has the lowest card
    if(playerA[0] < playerB[0])std::cout << "\nA\n"; else std::cout << "\nB\n";
}
Topic archived. No new replies allowed.