0

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
  • @πάντα ῥεῖ, that so-called duplicate doesn't solve this OP's problem. (See my comment below, which would be an answer if it could.) Voting to reopen. – TonyK Jan 02 '21 at 11:05
  • 1
    You need `new char[mapx * mapy]`, not `new char(mapx * mapy)`. What you did allocates a single `char` with the value `mapx * mapy`. – TonyK Jan 02 '21 at 11:05
  • Also, it's not cool to drool (all over the heap). – TonyK Jan 02 '21 at 11:06
  • 1
    `I know I can just make it a string but it takes more space` - You sure? Well, `+=` will not append anything, I guess they are not as cool as you thought. – Quimby Jan 02 '21 at 11:09
  • 1
    @TonyK Let's agree to disagree. OP's case is covered specifically [here](https://stackoverflow.com/a/4984228/1413395) in the duplicate. It's made clear, that one needs `new T[size]`, and not `T(size)` as the OP does. Also even if that would not have been covered in the duplicate, closing the question as _not reproducible or cased by a typo_ would have been appropriate as well. – πάντα ῥεῖ Jan 02 '21 at 11:18
  • @πάνταῥεῖ, that enormous answer (7kb!) says nothing about not using parentheses. It would be absolutely useless to our hapless OP. – TonyK Jan 02 '21 at 11:27
  • 1
    @TonyK OP asked about creating _dynamic arrays_ specifically. So I believe we can presume they're smart enough to find that section in the answers there, no? But anyways, they could edit their question to state that and why they disagree the duplicate fully answers their question. Also others besides you can put more reopen votes, if they think it's worth to have another answer for that specific case here. Nothing wrong so far IMO. – πάντα ῥεῖ Jan 02 '21 at 11:29
  • I solved this with a matrix, in the end thanks for the help tho – CrazyField Jan 06 '21 at 09:46

0 Answers0