0

试图将整数或字符串放入 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();

}

4

3 回答 3

0

尝试char()对您拥有的整数进行强制转换

于 2013-05-10T19:35:25.780 回答
0

唯一可以放入 char 数组的是 char。

char 是整数类型,将值与字符相关联。例如,在:

char n = q;

n 实际上的数值为 113(假设您使用 ASCII)。

当您将 67 作为整数添加到您的 char 数组(再次假设为 ASCII)时,它将显示为 C。这是因为 char 类型将该数值转换为字符。

类型转换适用于个位数整数,例如:

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

但是如果 myInt 中的数字超过一位,就会很混乱。

此外,您将无法将 int 的整个值存储在 char 数组中的单个位置,因为 int 的每个数字都需要 1 个“槽”。

是否可以执行以下操作:

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

在您使用的任何输出流上?这将允许您将 int 与其他所有内容一起粘贴。

编辑:

我有个主意。如何使用 stringstream,将 int 放入,从流中提取字符串,然后对于字符串中的每个项目,将其转换为 char,然后将每个单独的值按顺序放入数组中。我有点忙atm,但如果可以的话,稍后会发布一些代码。

于 2013-05-10T19:51:21.997 回答
0

我将发布一个新答案,因为它是一种完全不同的方法。这是使用字符串流的想法:

#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.

请记住,我尚未对此进行测试,并且需要进行调整以完美适应。

于 2013-05-11T20:11:31.317 回答