0
每次我尝试编译程序时,都会产生一个错误,指出字符数组中有太多字符。错误:字符串数组的初始化字符串太长C++
char map[10][10] = { "##########",
"#@ #",
"# #",
"######## #",
"# # #",
"# # # # #",
"#+# # # #",
"## # # #",
"# # #",
"#!########" };
该程序是一个像我们正在做计算机科学课的游戏,但我无法弄清楚错误。
整个程序在这里:
#include <iostream>
#include <windows.h>
using namespace std;
void Display(); //creates display function
bool stopGame = false;
int gamespeed = 100, hp = 100, maxHP = 100, XAmount = 9, YAmount = 9;
char map[10][10] = { "##########", //creates game map
"#@ #",
"# #",
"######## #",
"# # #",
"# # # # #",
"#+# # # #",
"## # # #",
"# # #",
"#!########" };
int main()
{
Display(); //runs display function
return 0;
}
void Display()
{
while(stopGame == false)
{
Sleep(gamespeed); //sets a gamespeed
system("cls"); //clears screen
for(int posY = 0; posY <= YAmount; posY++) //prints game map to screen
{
cout << map[posY] << endl;
}
for(int posY = 0; posY <= YAmount; posY++) //gets y position
{
for(int posX = 0; posX <= XAmount; posX++) //gets x position
{
switch(map[posY][posX])
{
case '@':
{
if(GetAsyncKeyState(VK_UP) != 0) //up movement
{
int y = (posY-1);
switch(map[y][posX])
{
case ' ':
{
map[posY][posX] = ' ';
posY -= 1;
map[y][posX] = '@';
}break;
case '+':
{
map[posY][posX] = 3;
map[posY][posX] = ' ';
posY -= 1;
map[y][posX] = '@';
}break;
}
}
if(GetAsyncKeyState(VK_DOWN) != 0) //down movement
{
int y2 = (posY+1);
switch(map[y2][posX])
{
case ' ':
{
map[posY][posX] = ' ';
posY += 1;
map[y2][posX] = '@';
}break;
case '+':
{
map[posY][posX] = 3;
map[posY][posX] = ' ';
posY += 1;
map[y2][posX] = '@';
}break;
}
}
if(GetAsyncKeyState(VK_LEFT) != 0) //move left
{
int x = (posX-1);
switch(map[posY][x])
{
case ' ':
{
map[posY][posX] = ' ';
posX -= 1;
map[posY][x] = '@';
}break;
case '+':
{
map[posY][posX] = 176;
map[posY][posX] = ' ';
posX += 1;
map[posY][x] = '@';
}break;
}
}
if(GetAsyncKeyState(VK_RIGHT) != 0) //move right
{
int x2 = (posX+1);
switch(map[posY][x2])
{
case ' ':
{
map[posY][posX] = ' ';
posX += 1;
map[posY][x2] = '@';
}break;
case '+':
{
map[posY][posX] = 3;
map[posY][posX] = ' ';
posX += 1;
map[posY][x2] = '@';
}break;
}
}
}break;
case '#': //sets ascii skin
{
map[posY][posX] = 219;
}break;
case '+': //sets ascii skin
{
map[posY][posX] = 3;
}
}
}
}
}
}
如果有人能帮助这将是真棒。
'“##########”'是11个字符。 – chris 2014-11-05 01:53:06
请记住为nul终结符'\ 0'允许一个额外的插槽。 – 2014-11-05 01:56:35