2014-09-03 61 views
-1
// deck of cards 
#include <iostream> 
#include <string> 
using namespace std; 

int main() 
{ 
    int i, j, k; 
    char arr[4][13][14] = 
    { 
     { 
      { heart one, heart two, heart three, heart four, heart five, heart six, heart seven, heart eight, heart nine, heart ten, heart jack, heart queen, heart king, heart ace } 
     }, 
     { 
      { diamond one, diamond two, diamond three, diamond four, diamond five, diamond six, diamond seven, diamond eight, diamond nine, diamond ten, diamond jack, diamond queen, diamond king, diamond ace } 
     }, 
     { 
      { club one, club two, club three, club four, club five, club six, club seven, club eight, club nine, club ten, club jack, club queen, club king, club ace } 
     }, 
     { 
      { spade one,spade two, spade three, spade four, spade five, spade six, spade seven, spade,eight, spade nine, spade ten, spade jack, spade queen, spade king, spade ace } 
     }, 
    }; 

    clrscr(); 
    printf(":::3D Array:::\n\n"); 
    for(i=0; i<4;i++) 
    { 
     for(j=0;j<13;j++) 
     { 
      for(k=0;k<14;k++) 
      { 
       printf("%d\t",arr[i][j][k]); 
      } 
      printf("\n"); 
     } 
     printf("\n"); 
    } 

    return 0; 
} 

進入心臟,鑽石,鐵鍬,俱樂部沒有被指定的錯誤。然而,我設置類型字符,有人可以給我一些關於如何解決這個問題的指針?我想爲我的一副撲克牌,4排(套裝),13列(2,3,...,ace)的3D陣列和14個數據位置(最長的是鑽石八,例如13個元素)。請幫忙!如何初始化一副撲克牌的3D陣列

+0

您的代碼深受其害,您有很多錯誤。 – 101010 2014-09-03 21:39:09

+0

「鐵鍬,心臟,俱樂部,鑽石,一,二,三......」的定義在哪裏? – 2014-09-03 21:40:07

+2

使用字符串代替char,並將這些名稱放在雙引號內。 – Omid 2014-09-03 21:44:12

回答

2

你可以開始從底部構建聲明,以幫助您 理解的聲明語法更好。

你會如何聲明一個14 char的數組?

char card[14] = "heart two"; 

現在,你將如何創建一個13這些數組?

char suite[13][14] = {"heart two", "heart three", "heart four" ...}; 

現在,你將如何創建一個4的數組?

char deck[4][13][14] = 
{ 
    {"heart two", "heart three", "heart four" ...}, 
    {"diamond two", "diamond three", "diamond four" ...}, 
    {"club two", "club three", "club four" ...}, 
    {"spade two", "spade three", "spade four" ...} 
}; 
+0

謝謝R Sahu,這種方式一定有意義。自從編程以來,它已經過去了幾年,最近又重新進入了它。非常感謝您的幫助! – matt 2014-09-03 22:28:28

+0

@matt,不客氣。很高興我能夠提供幫助。 – 2014-09-03 22:35:19

0

我猜你想要這樣的東西。另外,一副牌中沒有1個。

const char *arr[4][13]= 
{ 
    {"heart two", "heart three",...}, 
    {"diamond two", "diamond three",...}, 
    {"club two", "club three",...}, 
    {"spade two", "spade three",...} 
};