initializing array of char pointers

Im getting error from code thats in Deitels how to program c++.
The program assignment is to change deal routine to deal hand.
But the code in book that initialize char pointer arrays suit and face
will not compile getting compile errors for both initialize statements.
error 2440: initializing: cannot convert from 'const char[5] to 'const char*[13]
error 2440: initializing: cannot convert from 'const char[7] to 'const char*[4]

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
#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<time.h>

void shuffle(int[][13]);
void deal(const int[][13], int[][13]);

int main()
{
	using namespace std;

	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 };
	int handone[4][13] = { 0 };

	srand(time(0));
	shuffle(deck);
	deal(deck,handone);

	cin.clear();
	cin.ignore(255, '/n');
	cin.get();

	return 0;
}

void shuffle(int wDeck[][13])
{
	int row, column;

	for (int card = 1; card <= 52; card++){
		do {
			row = rand() % 4;
			column = rand() % 13;
		} while (wDeck[row][column] != 0);
		wDeck[row][column] = card;
	}
}


void deal(const int wDeck[][13], int whandone[][13])
{
	for (int i = 0; i < 4; i++){
		for (int j = 0; j <= 52; j++){
			switch (wDeck[i][j]){
			case 1 :
				whandone[i][j] = wDeck[i][j];
				break;
			case 2 :
				whandone[i][j] = wDeck[i][j];
				break;
			case 3 :
				whandone[i][j] = wDeck[i][j];
				break;
			case 4 :
				whandone[i][j] = wDeck[i][j];
				break;
			case 5 :
				whandone[i][j] = wDeck[i][j];
				break;
			default:
				break;

			}
		}
	}
}


 
Use curly brackets, not parenthesis when you create your char array thingy.

1
2
3
4
5
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" };
can i just interject and say its probably better to use std::array<std::string> here?
Yeh sure, but he probably has to follow a university assignment, or an assignment from a book, most people who post here do. So they cant or should not change.
Last edited on
Yeh sure, but he probably has to follow a university assignment, or an assignment from a book, most people who post here do. So they cant or should not change.


if its an assignment from a book, then he should most certainly do it. and they should most certainly change, even if they cant. they still need to be aware that there are better ways.
Topic archived. No new replies allowed.