-3

i am trying to make the beginnings of a very basic game i am trying to have it just make the border of the game witch will be x's and they player is H but when i build and run it the program will immediately crash please help

 #include <iostream>

 using namespace std;
 int cords[2];
 string map[6][6] =
{
 {"X","X","X","X","X","X"},
 {"X"," "," "," "," ","X"},
 {"X"," "," "," "," ","X"},
 {"X"," ","H"," "," ","X"},
 {"X"," "," "," "," ","X"},
 {"X","X","X","X","X","X"}
};
string input;
bool running = true;

void tick(){
if(input == "w"){
cords[1]++;
}
if(input == "s"){
cords[1] = cords[1] - 1;
}
if(input == "a"){
cords[0] = cords[0] - 1;
}
if(input == "d"){
cords[0]++;
   }
    }

void render(){
cout << map[1][1] << map[1][2] << map[1][3] << map[1][4] << map[1][5] << map[1][6]      <<endl
    << map[2][1] << map[2][2] << map[2][3] << map[2][4] << map[2][5] << map[2][6]   <<endl
    << map[3][1] << map[3][2] << map[3][3] << map[3][4] << map[3][5] << map[3][6] <<endl
 << map[4][1] << map[4][2] << map[4][3] << map[4][4] << map[4][5] << map[4][6] <<endl
 << map[5][1] << map[5][2] << map[5][3] << map[5][4] << map[5][5] << map[5][6] <<endl
 << map[6][1] << map[6][2] << map[6][3] << map[6][4] << map[6][5] << map[6][6] <<endl;

 }

 void run(){
 int ticks;
 int frames;

 tick();
 render();



  } 


 int main(){
 while(running == true){
 run();
 cin>> input;
 }
 }
maplelm
  • 61
  • 2
  • 2
  • 3

3 Answers3

2

Both map indeces must be between 0 and 5: map[6][3] is incorrect, for example

oleg
  • 148
  • 8
2

If you had written a loop to print out your 2d array, you may have avoided the error:

const int matSize = 6;

for (int i = 0; i < matSize; ++i) 
{ 
    for (int j = 0; j < matSize; ++j ) 
      cout << m[i][j]; 
    cout << "\n"; 
}
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
1

You should change your board to use string or char, not arrays of single character strings:

std::string map[6] = 
{
 "XXXXXX",
 "X    X",
 "X    X",
 "X H  X",
 "X    X",
 "XXXXXX",
};

char map_as_char_array[6][6] =
{
 {'X','X','X','X','X','X'},
 {'X',' ',' ',' ',' ','X'},
 {'X',' ',' ',' ',' ','X'},
 {'X',' ','H',' ',' ','X'},
 {'X',' ',' ',' ',' ','X'},
 {'X','X','X','X','X','X'}
};

The string data type is overkill for single letters.

Remember, the string type can be accessed as a single dimension array of characters.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154