Not sure what's happening...

I'm working on some code for a poker program. I'm in a very early stage, and right now I'm trying to test if two different values (that I get from the getValue function) are the same (a pair). I think I've assigned variables wrong or something. I've been trying to debug it and I'm still not sure what's going on. I have a few printf's included to see what's going on inside of the program. I've included everything I've done so far.

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
  #ifndef AdventureRPG_Poker_h
#define AdventureRPG_Poker_h
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
string cardOut;
int valueOut;
string suitOut;

int getValue(int a) {
    --a;
    int z = a % 13;
    z++;
    return z;
}
string getSuit(int a) {
    int z = a % 13;
    int x = a-z;
    x /= 13;
    if (x==0) {
        suitOut="Diamonds";
    } else if (x==1) {
        suitOut="Hearts";
    } else if (x==2) {
        suitOut="Spades";
    } else if (x==3) {
        suitOut="Clubs";
    }
    return suitOut;
}
bool checkPair(int a, int b, int c, int d, int e, int f, int g) {
    printf("%i %i %i  %i  %i  %i  %i   ", a, b, c, d, e, f, g);
    bool h;
    int array[5] = {c, d, e, f, g};
    for (int i=0; array[i]!=0; i++) {
        if (a==array[i]) {
            h=true;
            printf("%i = %i ", a, array[i]);
        } else if (b==array[i]) {
            h=true;
            printf("%i = %i ", b, array[i]);
        }
    }
    if (a==b) {
        h=true;
    }
    if (h!=true) {
        h=false;
    }
    return h;
}

#endif 

That's the header file Poker.h
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
#include "Poker.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define size 4
using namespace std;
char response[size];
string a;
string cardValues[14] = {"You'll never see me in the program!", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

int main(int argc, const char * argv[]) {
    int communityCards[5] = {28, 5, 6, 8, 48};
    int communityValues[5];
    for (int i = 0; communityCards[i]!=0; i++) {
        communityValues[i]=getValue(communityCards[i]);
    }
    string v = getSuit(25);
    int z = getValue(25);
    string u = getSuit(15);
    int w = getValue(15);
    bool x = checkPair(z, w, communityValues[0], communityValues[1], communityValues[2], communityValues[3], communityValues[4]);
    string b = cardValues[w];
    string a = cardValues[z];
    cout << "Your card is the " << a << " of " << v << " and the " << b << " of " << u << "." << endl;
    (x) ? cout << "You have a pair. " : cout << "You don't have a pair";
    return 0;
}


I know the program is confusing, but if anyone can help me out, that would be great.
Last edited on
First of all you should not declare global variables. Secondly that is really bad to do in a header. Think about it if you include the header you are also including the global variables..which could lead to multiple variables with same name. Secondly what is the purpose of your header file? AFAIK headers are meant meant for classes/structs prototypes so you can keep everything organized then you create a source file for that class and declare all the functions there. Then you include the header into your program that want's to use any of the object. I would also suggest looking into iterators as for loops also. I also see no purpose in your check pair function. Why are you using 7 ints? I could see two ints, or an int and a vector/array. Also in that function when you declare h you should initialize it to false that way if it isn't true by the time the return statement comes it will return false and you do not have to do an if statement and set it to false. Also you don't even technically need the bool h. Something like this works
1
2
3
4
5
 if( a == b )
{
    return( true );
}
return( false );

Return stops the function so..if it returns true then it will stop and will never return false otherwise it will by default return false.
Topic archived. No new replies allowed.