Poker game.

This was an assignment I had to do a while ago and already submitted. It is a simple poker game that the user and computer can play. I'm sure there is a way better way to do this but at the time it seemed like the best I could do given the specific instructions. Could someone please help me and explain what is wrong with my function compare_user_computer_cards and why it prints nothing. I need to fix the game so that it can determine who won the game, everything else works.

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;


void pause_215(bool have_newline)
{
if (have_newline) {
// Ignore the newline after the user's previous input.
cin.ignore(256, '\n');
}
// Prompt for the user to press ENTER, then wait for a newline.
cout << endl << "Press ENTER to continue." << endl;
cin.ignore(256, '\n');
}

// This function takes the user's poker cards, displays the match of the cards
// Parameters: the array and the size
// Returns: user_match_num, which is the highest number that matches in the user's cards
int user_compare_cards(int match_array[], int size)
{
int i = 0;
int j = 0;
int match = 0;
int user_match_num = 0;
// two for loops so that it can check to see which cards match
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (match_array[i] == match_array[j])
{
// assign user_match_num to card that matches for later use
user_match_num = match_array[i];
// Match is simply used as a system to decide the higher/better matches of the cards
match += 1;
}
}
}
cout << " " << endl;
if (match == 1)
{
cout << "Pair!" << endl;
}
else if (match == 2)
{
cout << "Pair of two sets!" << endl;
}
else if (match == 3)
{
cout << "Three of a kind!" << endl;
}
else if (match == 4)
{
cout << "Full House!" << endl;
}
else if (match == 6)
{
cout << "Four of a kind!" << endl;
}
else
{
cout << "no cards matched" << endl;
}
return user_match_num;
}

// This function is similar to the one above because it must once again find the match for the user, but this time it must return it
// Parameters: the array and the size
// Returns: user_match, the match number for the user
int user_compare_cards_return_match(int match_array[], int size)
{
int i = 0;
int j = 0;
int user_match = 0;
int user_match_num = 0;
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (match_array[i] == match_array[j])
{
user_match_num = match_array[i];
user_match += 1;
}
}
}

return user_match;
}


// This function takes the computer's cards and displays its matches
// Parameters: the array and the size
// Returns: computer_match_num, which is the highest number that matches in the computer's cards
int computer_compare_cards(int match_array[], int size)
{
int i = 0;
int j = 0;
int match = 0;
int computer_match_num = 0;
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (match_array[i] == match_array[j])
{
computer_match_num = match_array[i];
match += 1;
}
}
}
cout << " " << endl;
if (match == 1)
{
cout << "Pair!" << endl;
}
else if (match == 2)
{
cout << "Pair of two sets!" << endl;
}
else if (match == 3)
{
cout << "Three of a kind!" << endl;
}
else if (match == 4)
{
cout << "Full House!" << endl;
}
else if (match == 6)
{
cout << "Four of a kind!" << endl;
}
else
{
cout << "no cards matched" << endl;
}
return computer_match_num;
}

// This function is similar to the one above because it must once again find the match for the computer, but this time it must return it
// Parameters: the array and the size
// Returns: computer_match, the match number for the computer
int computer_compare_cards_return_match(int match_array[], int size)
{
int i = 0;
int j = 0;
int computer_match = 0;
int computer_match_num = 0;
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (match_array[i] == match_array[j])
{
computer_match_num = match_array[i];
computer_match += 1;
}
}
}

return computer_match;
}

// This function takes the match numbers from the computer and the user and the highest cards that matched from each, so that it can decide
// and output to the user whether they or the computer won.
// Parameters: user_match - the match number for the user's cards
// computer_match - the match number for the computer's cards
// user_match_num - the highest number that matches in the user's cards
// computer_match_num - the highest number that matches in the computer's cards
// Returns: nothing
int compare_user_computer_cards(int user_match, int computer_match, int user_match_num, int computer_match_num)
{
if (user_match > computer_match)
{
cout << "Congratulations, you won the game!" << endl;
}
else
{
if (computer_match > user_match)
cout << "Sorry, you lost the game!" << endl;
}

if (user_match == computer_match)
{
if (user_match_num > computer_match_num)
{
cout << "Congratulations, you won the game!" << endl;
}
else
{
if (computer_match_num < user_match_num)
{
cout << "Sorry, you lost the game!" << endl;
}
}
}
return 0;
}

int main()
{
srand(time(0));
string user_input;
int user_card;
while (user_input != "Q" || user_input != "q")
{
// Generate computer numbers
int computer_card1 = rand() % 13 + 1;
int computer_card2 = rand() % 13 + 1;
int computer_card3 = rand() % 13 + 1;
int computer_card4 = rand() % 13 + 1;
int computer_card5 = rand() % 13 + 1;
// Make sure all 5 are not the same
if (computer_card5 == computer_card4 && computer_card4 == computer_card3 && computer_card3 == computer_card2 && computer_card2 == computer_card1)
{
computer_card5 = (rand() % 13 + 1) != computer_card1;
}

// Generate user numbers
int user_card1 = rand() % 13 + 1;
int user_card2 = rand() % 13 + 1;
int user_card3 = rand() % 13 + 1;
int user_card4 = rand() % 13 + 1;
// Make sure all 5 are not the same
if (user_card4 == user_card3 && user_card3 == user_card3 == user_card2 && user_card2 == user_card1)
{
user_card4 = (rand() % 13 + 1) != user_card1;
}

// Get card from user
cout << "Let us start to play the game (Press Q to quit)" << endl;
cout << "Enter your card number (in the range of [1, 13]): ";
cin >> user_card;
if (user_card < 0 || user_card > 13)
{
cout << "Invalid range!" << endl;
}
if (cin.fail())
{
cin.clear();
cin >> user_input;
if (user_input == "Q" || user_input == "q")
{
cout << "Thank you for playing this game!" << endl;
cout << " " << endl;
pause_215(true);
return 0;
}
}
// Display user and computer's cards
cout << " " << endl;
cout << "Your five cards are" << " " << user_card << " " << user_card1 << " " << user_card2 << " " << user_card3 << " " << user_card4 << endl;
cout << " " << endl;
cout << "My five cards are" << " " << computer_card1 << " " << computer_card2 << " " << computer_card3 << " " << computer_card4 << " " << computer_card5 << endl;
cout << " " << endl;
// Initialize all variables
int i = 0;
int j = 0;
int user_match_num = 0;
int computer_match_num = 0;
int user_match = 0;
int computer_match = 0;
int size = 5;

// Create array for user
int user_match_array[] = { user_card, user_card1, user_card2, user_card3, user_card4 };
// Take and display the user's highest matched card
int user_ans = user_compare_cards(user_match_array, size);
cout << "The ranking of your poker cards is: " << user_ans << endl;
user_compare_cards_return_match(user_match_array, size);

// Create array for computer
int computer_match_array[] = { computer_card1, computer_card2, computer_card3, computer_card4, computer_card5 };
// Take and display the computer's highest matched card
int computer_ans = computer_compare_cards(computer_match_array, size);
cout << "The ranking of my poker cards is: " << computer_ans << endl;
computer_compare_cards_return_match(computer_match_array, size);
cout << " " << endl;
// Compare user and computer's matches and display who won
compare_user_computer_cards(user_match, computer_match, user_match_num, computer_match_num);
cout << " " << endl;
}
// End program
pause_215(true);
return 0;
}
My first suggestion is that you use code tags when posting code.

Next you may want to increase your compiler warning levels and fix any and all warnings generated. Look what my compiler says about your program.
main.cpp||In function ‘int user_compare_cards(int*, int)’:|
main.cpp|32|warning: declaration of ‘i’ shadows a previous local [-Wshadow]|
main.cpp|26|note: shadowed declaration is here|
main.cpp|34|warning: declaration of ‘j’ shadows a previous local [-Wshadow]|
main.cpp|27|note: shadowed declaration is here|
main.cpp|26|warning: unused variable ‘i’ [-Wunused-variable]|
main.cpp|27|warning: unused variable ‘j’ [-Wunused-variable]|
main.cpp||In function ‘int user_compare_cards_return_match(int*, int)’:|
main.cpp|86|warning: declaration of ‘i’ shadows a previous local [-Wshadow]|
main.cpp|81|note: shadowed declaration is here|
main.cpp|88|warning: declaration of ‘j’ shadows a previous local [-Wshadow]|
main.cpp|82|note: shadowed declaration is here|
main.cpp|81|warning: unused variable ‘i’ [-Wunused-variable]|
main.cpp|82|warning: unused variable ‘j’ [-Wunused-variable]|
main.cpp|84|warning: variable ‘user_match_num’ set but not used [-Wunused-but-set-variable]|
main.cpp||In function ‘int computer_compare_cards(int*, int)’:|
main.cpp|112|warning: declaration of ‘i’ shadows a previous local [-Wshadow]|
main.cpp|107|note: shadowed declaration is here|
main.cpp|114|warning: declaration of ‘j’ shadows a previous local [-Wshadow]|
main.cpp|108|note: shadowed declaration is here|
main.cpp|107|warning: unused variable ‘i’ [-Wunused-variable]|
main.cpp|108|warning: unused variable ‘j’ [-Wunused-variable]|
main.cpp||In function ‘int computer_compare_cards_return_match(int*, int)’:|
main.cpp|164|warning: declaration of ‘i’ shadows a previous local [-Wshadow]|
main.cpp|159|note: shadowed declaration is here|
main.cpp|166|warning: declaration of ‘j’ shadows a previous local [-Wshadow]|
main.cpp|160|note: shadowed declaration is here|
main.cpp|159|warning: unused variable ‘i’ [-Wunused-variable]|
main.cpp|160|warning: unused variable ‘j’ [-Wunused-variable]|
main.cpp|162|warning: variable ‘computer_match_num’ set but not used [-Wunused-but-set-variable]|
main.cpp||In function ‘int main()’:|
main.cpp|218|warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]|
main.cpp|244|warning: self-comparison always evaluates to true [-Wtautological-compare]|
main.cpp|244|warning: suggest parentheses around comparison in operand of ‘==’ [-Wparentheses]|
main.cpp|280|warning: unused variable ‘i’ [-Wunused-variable]|
main.cpp|281|warning: unused variable ‘j’ [-Wunused-variable]|
||=== Build finished: 0 error(s), 23 warning(s) (0 minute(s), 1 second(s)) ===|


Most of the "shadowed declarations" are being caused because you are declaring all of your variables in one big glob at the beginning of your function, then later you re-declare the variables later in the function:
1
2
3
4
5
6
7
8
9
10
int user_compare_cards(int match_array[], int size)
{
    int i = 0;
    int j = 0;

...
    // two for loops so that it can check to see which cards match
    for(int i = 0; i < size; i++) // This causes the "shadowed declaration" warning.
    {
        for(int j = i + 1; j < size; j++)

In C++ you can just declare and initialize the loop variables in the loop initialization section if you're only going to use these variables in the body of the loop, no need for the original declarations. If you do want to use these outside of the loop then you don't need to declare the variable in the loop initialization section, just initialize the variable:

1
2
3
4
5
6
7
int i;
for(i = 0; i < 10; ++i)
{
...
}
...
// You can use i outside of the loop. 


Look at this snippet:
if(user_card4 == user_card3 && user_card3 == user_card3 == user_card2 && user_card2 == user_card1)

This is not how you compare items in C++, the first comparison is correct but the second is suspect.
Topic archived. No new replies allowed.