2013-05-10 19 views
0

試圖將整數或字符串放入2D char數組中,但我沒有運氣。嘗試將整數轉換爲一個字符串,然後將其放入數組中,但不確定是否可行。作爲一個相對較新的程序員,我想不出有什麼其他方法可以將整數插入到數組中,所以任何幫助都很棒!下面是2D數組下面的部分,我想在'ScoreInt'處放置一個整數,如果有任何幫助,我會在類中設置變量。在某些座標中放置二維數組中的整數或字符串?

Level::Level(Snake *s, Item *i) 

/// TITLE SECTION 

for(int y=1; y<8 ; y++) 
{ 
    map[y][0]=' '; 

    for(int x=1; x<70 ; x++) 
    { 
     ///Letter 'S' 

     map[y][x]=' '; 
     map[2][19]= 177; 
     map[2][20]= 176; 
     map[2][21]= 176; 
     map[2][22]= 176; 
     map[2][23]= 177; 
     map[3][19]= 176; 
     map[4][19]= 178; 
     map[4][20]= 176; 
     map[4][21]= 176; 
     map[4][22]= 178; 
     map[4][23]= 177; 
     map[5][23]= 176; 
     map[6][23]= 176; 
     map[6][22]= 177; 
     map[6][21]= 177; 
     map[6][20]= 176; 
     map[6][19]= 178; 

     ///Letter 'N' 

     map[2][26]= 178; 
     map[3][26]= 178; 
     map[4][26]= 176; 
     map[5][26]= 177; 
     map[6][26]= 176; 
     map[2][27]= 176; 
     map[3][27]= 177; 
     map[4][28]= 176; 
     map[5][29]= 176; 
     map[6][29]= 177; 
     map[2][30]= 176; 
     map[3][30]= 176; 
     map[4][30]= 178; 
     map[5][30]= 176; 
     map[6][30]= 177; 

     ///Letter 'A' 

     map[2][33]= 178; 
     map[2][34]= 176; 
     map[2][35]= 178; 
     map[2][36]= 176; 
     map[2][37]= 177; 
     map[3][37]= 176; 
     map[4][37]= 176; 
     map[5][37]= 176; 
     map[6][37]= 178; 
     map[3][33]= 176; 
     map[4][33]= 177; 
     map[5][33]= 176; 
     map[6][33]= 177; 
     map[4][34]= 176; 
     map[4][35]= 177; 
     map[4][36]= 177; 

     ///Letter 'K' 

     map[2][40]= 176; 
     map[3][40]= 177; 
     map[4][40]= 176; 
     map[5][40]= 178; 
     map[6][40]= 176; 
     map[4][41]= 176; 
     map[3][42]= 178; 
     map[2][43]= 177; 
     map[5][42]= 178; 
     map[6][43]= 177; 

     ///Letter 'E' 

     map[2][46]= 177; 
     map[2][47]= 176; 
     map[2][48]= 176; 
     map[2][49]= 176; 
     map[2][50]= 177; 
     map[3][46]= 177; 
     map[4][46]= 176; 
     map[4][47]= 176; 
     map[4][48]= 177; 
     map[5][46]= 176; 
     map[6][46]= 176; 
     map[6][47]= 178; 
     map[6][48]= 178; 
     map[6][49]= 176; 
     map[6][50]= 178; 
    } 
} 

for(int x=0; x<71; x++) 
{ 
    map[8][0]= 218; 
    map[8][x]= 196; 
    map[8][70]= 191; 
} 

/// INFORMATION SECTION (SCORE, LIVES) 

for(int y=9; y<12 ; y++) 
{ 
    map[y][0]= 179; 

    for(int x=1; x<70 ; x++) 
    { 
     map[y][x]=' '; 

     map[10][20] = 'S'; 
     map[10][21] = 'C'; 
     map[10][22] = 'O'; 
     map[10][23] = 'R'; 
     map[10][24] = 'E'; 
     map[10][25] = ':'; 

     map[10][27] = ScoreInt; 

     map[10][40] = 'L'; 
     map[10][41] = 'I'; 
     map[10][42] = 'V'; 
     map[10][43] = 'E'; 
     map[10][44] = 'S'; 
     map[10][45] = ':'; 
    } 

    map[y][70]= 179; 
} 

for(int x=0; x<71; x++) 
{ 
    map[12][0]= 195; 
    map[12][x]= 196; 
    map[12][70]= 180; 
} 

/// GAME SECTION 

for(int y=13; y<42 ; y++) 
{ 
    map[y][0]= 179; 

    for(int x=1; x<70 ; x++) 
    { 
     map[y][x]=' '; 
    } 

    map[y][70]= 179; 
} 

for(int x=0; x<71; x++) 
{ 
    map[42][0]= 192; 
    map[42][x]= 196; 
    map[42][70]= 217; 
} 

///FOOTER 

/*for(int y=43; y<44 ; y++) 
{ 
    map[y][0]= "FOOTER"; 
}*/ 

refresh_display = false; 

lSnake = s; 
map[lSnake->getLocation().y][lSnake->getLocation().x]=lSnake->getSnakeAppearanceRight(); 

lItem = i; 
map[lItem->getLocation().y][lItem->getLocation().x]=lItem->getItemAppearance(); 

}

回答

0

嘗試鑄造的整數char()你有

+0

試過,並沒有工作。 – TRAUMA 2013-05-10 19:46:21

0

,你可以把一個字符數組的唯一事情是一個char。

char是一個整數類型,並將一個值與一個字符關聯。例如,在:

char n = q; 

n實際上有一個數值113(假設您使用ASCII)。

當您添加時,將67表示爲整數(如果再次假設爲ASCII),它將顯示爲C.這是因爲char類型將該數值轉換爲字符。

型鑄造將爲個位數整數的工作,如:

chars[x][y] = (char)myInt; 

但會亂碼如果在敏不止一個數字。

此外,您將無法將int的整個值存儲在char數組中的單個位置,因爲int的每個數字都需要1個'slot'。

是否有可能做這樣的事情:

cout << (everything_up_to_map[10]25) << ScoreInt << (map[10][40]_onwards); 

你使用哪個輸出流?這可以讓你把int和其他所有東西都粘在一起。

編輯:

我有一個想法。如何使用stringstream,放入int,從流中提取字符串,然後爲字符串中的每個項目,將其轉換爲char,然後將這些單個值按順序放入數組中。我有點忙,但如果可以的話,稍後會發布一些代碼。

+0

我明白你在說什麼,但我不確定我如何將它分成兩部分,這兩部分被認爲是變量。我在課堂上試圖把它們分開,但這可能不是正確的做法,對此有何幫助? – TRAUMA 2013-05-10 21:28:25

+0

據我所知,這是爲了在一定的佈局輸出。你究竟做了什麼,以什麼方式行不通?這種信息真的有幫助! – Joe 2013-05-10 23:45:30

+0

對不起,我回到了我的地圖存儲在課程中的地方,並設置了兩個變量,這兩個變量都是地圖,但顯然名稱不同,然後使用這兩個地圖我嘗試存儲這兩個地方,以便我可以顯示這兩個部分之間的分數,但沒有運氣,所以把我的代碼改回原來的樣子。 – TRAUMA 2013-05-11 14:54:55

0

我會發佈一個新的答案,因爲它是一個非常不同的方法。這裏是一個想法,使用stringstreams:

#include <sstream> 
//Your includes 
//& other code 
int scoreInt; 

//This bit takes your int, and places in a string as a chars, rather than a value 
stringstream ss; 
ss << scoreInt; 
std::string tmpStr = ss.str(); //A temporary string for the next bit, 
           //Now with added int! 

char digits[maxDigits] = {' ', ' ', ' ', ' ', ' '}; //Use the max number of 
                //digits you have room for 

//Take each digit from the string, put it in digits 
//So you don't overfill your array accidentally 
//This should automatically truncate the number 
for(int i = 0; i < maxDigits; i++) 
{ 
    digits[i] = tmpStr[i]; 
} 

map[1stSlot][ForScore] = digits[0]; 
map[2ndSlot][ForScore] = digits[1]; 
//So on, so on. 

熊,我沒有測試過這個心思,將需要調整以適應完美。

相關問題