Characters

I am making a simple black jack game just A12345678910JQKA (1-14)

using

srand((unsigned)time(0));

randomNumberadd = 1 + rand() %14;

numbers are generating fine. but say 11 is randomly generated

11 = J

how do i get J to display and not 11?

Please help.

Thanks!


One option is to use an array of strings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

const string card_num_to_str[14]=
{
    "", "A", "2", "3", "4", "5", "6",
    "7", "8", "9", "10", "J", "Q", "K"
};

int main()
{
    for (int i=1; i<=13; i++)
        cout << i << " -> " << card_num_to_str[i] << endl;

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}

More info on arrays -> http://cplusplus.com/doc/tutorial/arrays/
More info on strings -> http://cplusplus.com/reference/string/string/
I am just learning C++ and I may be overly focused on this topic because I am studying it now. But your problem seems like it would be solved using an enumerator. You would set it up with 11 = Jack, 12 = Queen, 13 = King, 14 = Ace High. I can't write the code, but you can look up how to use enumerators. This site's tutorial explains it.
Topic archived. No new replies allowed.