Being able to type int or char

I'm currently writing a program that will half simulate a modified version of poker. I am in a very early stage of the program which will be incorporated in a larger program later on. I have a couple of functions in Poker.h that when I submit a number will give me the suit and a number from 1-13. I want to be able to convert that number to either say, "Ace", "Jack", "Queen", "King", or the number. I don't want to have to say, "if (z==2) {a="2"} else if (z==3) {a="3")" etc. If anyone has ideas, that would be helpful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "Poker.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define size 4
using namespace std;
char response[size];


int main(int argc, const char * argv[])
{
    string v = getSuit(13);
    int z = getValue(13);
    cout << z << endl;
    cout << v << endl;
}
...idea. Make an array to hold those string values, 0-12. Then just plug it into the array itself (minus one) to get the card value.
I'm pretty sure you don't understand the code. I very well may just be confused by what you're saying, but I included Poker.h here.

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
#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;
string of = " of ";

int getValue(int a) {
    int z = a % 13;
    z++;
    int x = a-z;
    x /= 13;
    int b = z;
    valueOut = b;
    return valueOut;
}
string getSuit(int a) {
    int z = a % 13;
    int x = a-z;
    x /= 13;
    if (x==0) {
        suitOut="Diamond";
    } else if (x==1) {
        suitOut="Heart";
    } else if (x==2) {
        suitOut="Spade";
    } else if (x==3) {
        suitOut="Club";
    }
    return suitOut;
}

#endif 
Well, what I mean by an array is an array of strings, with ace being at the 0th memory spot and king being at the 12th. When you plug in the card value (minus one), it will give you what card it is, i.e. ace, two, three, et cetera. No need for a long switch or if-else chain.
You could do a switch/case with a default statement based on the result of getValue(), but that is basically the same as writing a few if/else branches. You could try making a map (though since you have integer keys it can be a standard array), as I (think) was being suggested above, that goes from a number to the string you want to output.
Thank you, this did end up working.
Topic archived. No new replies allowed.