Can someone help me finish this code off. Its a simple program that prompts two players to pick a card. Each picks between 0-51. An array is holding each of the cards by rank and suit using a struct. Everything works great except the greater_than function. The only issue with it is getting the Ace which is 1, to "beat out" the other cards. The high card as it stands now is the king which is 13. The mix function is commented on purpose so I can find the Ace for testing purposes. It is just that one glitch that is getting me. Please help!!! I'm pretty new to this, so go easy on me. I know its probably a simple fix.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;



struct card {
       int rank;
       char suit;
       };


void create (card deck [52]);
void mix (card deck [52]);
bool greater_than (card c1, card c2);
void print_card (card& c1);
void print_deck (card deck [52]);

int main(int argc, char *argv[])
{
  srand (time(0));
  card my_array [52];
  int p1;
  int p2;

      
    
    
      cout << "Player 1: Pick the position of your card in the deck: ";
      cin >> p1;
      
      cout << "Player 2: Pick the position of your card in the deck: ";
      cin >> p2;
      
      cout << endl;
    
    create (my_array);
   // mix (my_array);
    cout << "Player 1 Card: ";
    print_card (my_array[p1]);
    cout << "Player 2 Card: ";
    print_card (my_array[p2]);
    cout << endl;
    greater_than (my_array [p1], my_array [p2]);                
    print_deck (my_array);
    
  
    system("PAUSE");
    return EXIT_SUCCESS;
}

void create (card deck [52]) 
{
 
 int r;
 char s;
 int index;
 
 index = 0;
 
     for (r = 1; r <= 13; r++)
        for (s = 3; s <= 6; s++)
        {
        deck[index].rank = r;
         deck[index].suit = s;
        index++;
           
        }      
        
}
/*void mix (card deck [52]) 
{
  int i;
  int x;
  card temp;
  
         for (i = 0;i < (52 - 1);i++)
         { 
            x = i + (rand() % (52 - i));
            temp = deck [i]; 
            deck [i] = deck [x]; deck[x] = temp; 
            }
  
     
}*/
bool greater_than (card c1, card c2)
{
     
  if (c1.rank > c2.rank)
     
    return cout << "Player 1 Wins!" << endl << endl;
          else
    return cout << "Player 2 Wins!" << endl << endl; 
           
  
}
void print_card (card& c1)
{
  
    if (c1.rank == 1)
    {  
    
     cout << "Ace" << " of " << c1.suit << endl;
     
     }else if (c1.rank == 11){
     cout << "Jack" << " of " << c1.suit << endl;
     
     }else if (c1.rank == 12){
     cout << "Queen" << " of " << c1.suit << endl;
     
     }else if (c1.rank == 13){
     cout << "King" << " of " << c1.suit << endl;
    
     }else{
     cout << c1.rank << " of " << c1.suit << endl;
     }  
}


void print_deck (card deck [52]) 
{
  int index;

  index = 0;

  cout << "\nThe Deck is: " << endl;
  for (index = 0;index < 52;index++)
  
        print_card (deck[index]);
     
}

The logic checks might be reducible, but test this out.

#include <iostream>
using std::cout;
using std::endl;

int main(){

  int a(4),b(1);

  if ( (a == 1 && b != 1 ) || (b != 1 &&  a > b)) {
     cout << "A wins" << endl;
  }
  else if ( (b == 1 && a != 1) || (a != 1 && b > a ))  {
     cout << "B wins" << endl;
  }
  else {
     // You need to add code for tie breaker
     cout << "Need to check the suit to break tie" << endl;
  }

  return 0;
}

An issue that is not part of your question: what about ties? Also, this is a boolean function. It should return true or false.

return cout << "Player 1 Wins!" << endl << endl

doesn't make sense.


As to your question, either make an Ace's rank equal to 14 or make a special case for the Ace in the greater_than function.

bool greater_than (card c1, card c2)
{
    // again, what about ties?
    if(c1.rank == 1 && c2.rank != 1)
        return true;
    if (c1.rank > c2.rank)
        return true;
    else
        return false;
}

If it just returns true or false, then do I use an if statement in main for the cout statements that I need? I need it to state which player wins. As far as the draw I thought about that and posed the question to my professor and it was not part of the problem he wanted to see. We were learning structs and he kept it simple for us. Unfortunately Im a bit more simple with C++. Thank you for the help already. What do you mean by special case?

An issue that is not part of your question: what about ties? Also, this is a boolean function. It should return true or false.

return cout << "Player 1 Wins!" << endl << endl

doesn't make sense.


As to your question, either make an Ace's rank equal to 14 or make a special case for the Ace in the greater_than function.

bool greater_than (card c1, card c2)
{
    // again, what about ties?
    if(c1.rank == 1 && c2.rank != 1)
        return true;
    if (c1.rank > c2.rank)
        return true;
    else
        return false;
}

>> If it just returns true or false, then do I use an if statement in main for the cout statements that I need? I need it to state which player wins.

Fine. Make it a void function, get rid of the return statements, and call the function "print_winner".

Or keep it as a boolean function, have it return true or false, then harness that value in main.

bool player1_is_winner = greater_than(my_array [p1], my_array [p2]);
if(player1_is_winner)
{
    cout << "Player 1 won." << endl;
}
else
{
    cout << "Player 1 did not win." << endl;
}

>> What do you mean by special case?

I mean the Aces are checked as Aces, not by whether they are "greater" than another card. Ace wins, period, if the other card is not an Ace. Actually, I left something before. Or, as mentioned, make Aces 14 and there are no special cases.

bool greater_than (card c1, card c2)
{
    // check "Ace" special cases
    // c2 is an Ace
    if(c2.rank == 1)
        return false; // nothing beats an Ace.
    // c1 is an Ace
    if(c1.rank == 1)
        return true; // Ace beats everything but another Ace.


    // now check the caseswhere nothing is an Ace
    if (c1.rank > c2.rank)
        return true;
    else
        return false;
}

I see what you are saying. I was right on the edge of this after your last post, but still wasnt there. This makes perfect sense now. Thank you and I appreciate the time you gave me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.