I am trying to create a dynamic char array in c++ however when I make it, it just generates some errors because I'm trying to add chars to it, I'll enter the code here. I know I can just make it a string but it takes more space and it's much cooler with a dynamic array :sunglasses:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int mapx; int mapy; int xpl = 8; int ypl = 8; char action = ' '; int score = 0; // var declaraction
cout << "enter the x and then y of the map \n";
cin >> mapx;
cin >> mapy;
xpl = mapx / 2;
ypl = mapy / 2;
int coinx = rand() % mapx;
int coiny = rand() % mapy;
char* map = new char(mapx * mapy);
while (action != 'e') { // runs the game
for (int y = 0; y <= mapy; y++) { // generates the y of the map
for (int x = 0; x <= mapx; x++) { // generates the x of the map + the player
if (x == xpl && y == ypl) {
map += "X";
}
else if (x == coinx && y == coiny) map += "C";
else {
map += "_";
}
} // end of x+player for
map += "\n";
if (xpl == coinx && ypl == coiny) { //checks if player hit coin
coinx = rand() % mapx;
coiny = rand() % mapy;
score++;
}
} // end of y for
cout << map + "Enter an action ";
cin >> action; // movement
if (action == 'w') ypl--;
else if (action == 's') ypl++;
else if (action == 'd') xpl++;
else { xpl--; }
// checks what the move is
map = ""; //resets the map
} // end of game
cout << "GAME OVER " << score;
} // end of main