Output deck of cards not aligned, why?

Trying to display deck of cards, but they aren't side by side; rather they have gaps in the next line down.

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

void shuffleAndDeal(int[][13], const char*[], const char *[]);
int main()
{
	const char *suit[4] = { "Hearts", "Diamonds", "Clubs", "Spades" };
	const char *face[13] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

	int deck[4][13] = { 0 };
	srand(time(0));
	shuffleAndDeal(deck, face, suit);
	
	system("pause");
}

void shuffleAndDeal(int deck[][13], const char*face[], const char *suit[])
{

	int facerand, suitrand;
	for (int i = 1; i <= 26; i++)
	{
		facerand = rand() % 13;
		suitrand = rand() % 4;
		cout << face[facerand] << " of " << suit[suitrand] << endl;

		facerand = rand() % 13;
		suitrand = rand() % 4;

		cout << "\t\t\t" << face[facerand] << " of " << suit[suitrand] << endl;
	}

}
Last edited on
Your code is outputting a newline at both line 28 and 33. You only need one line break per two items.

The other problem is the use of the tab character for alignment. It can be very difficult to get a consistent alignment that way. I'd suggest something using setw() instead.

Here the problem with setw() is that you want it to apply to three items as a group, hence I suggest a temporary buffer to join them. Below I used a string, there are other methods.

Also, avoid duplicated code as at lines 26-28 and 39-33,

Of course there are other problems. parameter int deck[][13] is unused, and the cards generated contain duplicates.

Here a suggested way of aligning the results. Other issues remain and still need fixing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void shuffleAndDeal(int deck[][13], const char*face[], const char *suit[])
{

    cout << left;
    
    const string of = " of ";
    
    for (int i = 1; i <= 52; i++)
    {
        int facerand = rand() % 13;
        int suitrand = rand() % 4;
        string temp = face[facerand] + of + suit[suitrand];
        cout << setw(24) << temp;
        if (i%2 == 0)
            cout << endl;
    }

}
Last edited on
closed account (48T7M4Gy)
Another way using cstring funcionality to copy and join C-style strings
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

#include <cstring> // <--

using namespace std;

void shuffleAndDeal(int[][13], const char*[], const char *[]);
int main()
{
	const char *suit[4] = { "Hearts", "Diamonds", "Clubs", "Spades" };
	const char *face[13] = { "Ace", "Deuce", "Three", "Four", "Five", "Six",
 "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

	int deck[4][13] = { 0 };
	srand(time(0));
	shuffleAndDeal(deck, face, suit);
	
	system("pause");
}

void shuffleAndDeal(int deck[][13], const char*face[], const char *suit[])
{

	int facerand, suitrand;
	
	char temp[50]; // <--
	
	for (int i = 1; i <= 26; i++)
	{
		facerand = rand() % 13;
		suitrand = rand() % 4;
		
		strcpy(temp,face[facerand]); // <-- etc
		strcat(temp, " of ");
		strcat(temp, suit[suitrand]);
		
		cout << left << setw(20) << temp;
		
		facerand = rand() % 13;
		suitrand = rand() % 4;

		cout << face[facerand] << " of " << suit[suitrand] << endl;
	}
}
Last edited on
Topic archived. No new replies allowed.