How to output an int as a string

Hello,

I am new to programming and am attempting a c++ exercise.

In a simple card game between 2 players each player is dealt a card in turn, the
rank of which should be output.
The ranks of the cards are compared and the player with the higher card gets a
point and the program reports which player has won the round (in this game Ace
is low). If the cards have the same rank, the program reports a draw and neither
player gets the point.
The game finishes when each player has been dealt 7 cards and the players'
scores and overall winner is then output. Again, if there is a draw, the program
must report this.



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
 //Task 2: Card game problem solving
#include <iostream>
#include "Header.h"
#include <math.h>
#include <time.h>
#include <string>
using namespace std;
int main()
{
	int p1card = 0, p2card = 0, count = 0;
	int p1point = 0, p2point = 0;
	int p1score = 0, p2score = 0;

	srand(time(NULL)); //Initalise random seed

	string player1, player2;

	cout << "Enter player 1 name:\t";
	getline(cin, player1);
	cout << "Enter player 2 name:\t";
	getline(cin, player2);


	do 
	{
		p1card = rand() % 13 + 1;
		p2card = rand() % 13 + 1;
		count++;

		/*if (p1score == 1) {
			return('Ace');
		}
		else if (p1score == 11) {
			return('Jack');
		}
		else if (p1score == 12) {
			return('Quen');
		}
		else if (p1score == 13) {
			return('King');
		}*/

		cout << "\nRound no.\t" << count << endl;
		cout << "==================";
		cout << endl << player1 << " was dealt: " << p1card;
		cout << endl << player2 << " was dealt: " << p2card << endl;

		if (p1card == p2card)
		{
			cout << "Round " << count << " ended in a draw: " << endl;
		}
		else if (p1card > p2card)
		{
			cout << player1 << " wins the round!" << endl;
			p1score++;
		}
		else if (p2card > p1card)
		{
			cout << player2 << " wins the round!" << endl;
			p2score++;
		}
	 
		 
	} while (count != 26);

	p1(p1score, p2score, player1, player2);
	p2(p1score, p2score, player1, player2);

	cout << endl << endl;

	system("PAUSE");
	return 0;
}

void p1(int p1score, int p2score, string player1, string player2)
{
	if (p1score > p2score)
	{
		cout << "\n\nFinal scores \n";
		cout << "================\n";
		cout << player1 << " score: " << p1score << endl;
		cout << player2 << " score: " << p2score << endl;

		cout << player1 << " wins the game! Goodbye!\n";
	}
}

void p2(int p1score, int p2score, string player1, string player2)
{
		if (p2score > p1score)
		{
			cout << "\n\nFinal scores \n";
			cout << "================\n";
			cout << player1 << " score: " << p1score << endl;
			cout << player2 << " score: " << p2score << endl;

                        cout << player2 << " wins the game! Goodbye!\n";
		}
}


This is my code so far. I would like to know how to display "11, 12, 13" as "Jack, Queen, King".

How would you output an int as a string in c++?

Cheers
Last edited on
I would like to know how to display "11, 12, 13" as "Jack, Queen, King".

Use an array.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

using namespace std;

int main()
{
    const string names[] = { "", "One", "Two", "Three", "Four", "Five", 
        "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
        
    for (int i=1; i<=13; ++i)
        cout << i << "  " << names[i] << '\n';   
}
1  One
2  Two
3  Three
4  Four
5  Five
6  Six
7  Seven
8  Eight
9  Nine
10  Ten
11  Jack
12  Queen
13  King
Last edited on
Using an array seems like a good idea but how would I randomise the values in the string?
I set the boundries from 1 - 13 with the code below, but how would I do that in this situation?
This is the thing thats confusing me

1
2
3
4
 
p1card = rand() % 13 + 1;
p2card = rand() % 13 + 1;
count++;


how would I randomize the values in the string?

You don't want to randomize the string array. You want to randomize the indexes to it.
Note how Chervil used names[i] in line 12 to print the i'th name.
1
2
3
4
5
6
7
8
9
10
int main()
{	const string names[] = { "", "One", "Two", "Three", "Four", "Five",
		"Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
	int p1card, p2card;

	p1card = rand() % 13 + 1;
	p2card = rand() % 13 + 1; 
	cout << "Player 1 drew a: " << names[p1card];
	cout << "Player 2 drew a: " << names[p2card];
}



Oh I finally understand it, thank you so much for the help guys!

You basically need to call the array and link it with the int by

names[p1card];

However is there any reason why {""} is the the first element of the array here?
const string names[] = {""}

Why not const string names[] = {"one", etc};
Why does this give a memory leek? Is it because rand()13 + 1; is somehow 14 elements of an array and it is skipping the first element ""...going to the second element which is "one" to the 14th element which is 13?

rand() % 13 + 1;

is rand just the values from 1 - 13? Or are these elements of the array?
Last edited on
However is there any reason why {""} is the the first element of the array here?
 
const string names[] = {""}

It was put there because array indexing starts from zero, not 1.

I assumed that you would be interested in cards numbered from 1 to 13. So I inserted a 14th element which will never be used, but it keeps the others in the required positions.

Any questions about memory leak are probably a misuse of terminology here, since there is no dynamically allocated memory involved. On the other hand, with arrays, there is always a risk of out of bounds access - if the requested index is either negative, or greater than the last element position. But that's not a leak, and it should not happen in carefully written and tested code.

p1card = rand() % 13 + 1;
rand() % 13 will give a value in the range 0 to 12 inclusive. Then 1 is added, making it 1 to 13.

Note that for an array of fourteen elements, valid subscripts are in the range 0 to 13. And in this case we are not intending to use element zero.
Ahh I finally understand it now, thank you very much for the help and in-depth clarification. It is very much appreciated!

Cheers
Topic archived. No new replies allowed.