-1
void Map::LoadMap(std::string path, int sizeX, int sizeY) {
    char c;
    std::fstream mapFile;
    mapFile.open(path);
    
    int srcX, srcY;
    
    for(int y = 0; y < sizeY; y++) {
        for(int x = 0; x < sizeX; x++) {
            mapFile.get(c);
            srcY = atoi(&c) * 32;
            
            mapFile.get(c);
            srcX = atoi(&c) * 32;
            ks::Game::AddTile(srcY, srcX, x * 32, y * 32);
            std::cout << "X: " << srcX << " Y:" << srcY << std::endl;
            mapFile.ignore();
        }
    }
    mapFile.close();
}

I wont Post my whole map file but the layout is like so

00,01,02,03,44,00,00,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44

I am just curious my system is a Mac, I did a similar program on windows and it's read this code character by character however on XCode it is reading the file whole number by whole number and not character by character, Instead of it grabbing the first digit example 4 and multiplying it be 32 it is grabbing 44 then multiplying it by 32 instead.

I simply just want the First digit to be used as a Y coord and the second to be used as a X coord and the "," is skipped.

Apologise in advance if there is something I am overlooking however two minds are better then one any answer would be very much appreciated.

Kai Mc
  • 21
  • 7

1 Answers1

2

Your code invokes Undefined Behaviour as atoi expects a null-terminated string, which &c is clearly not, it's a pointer to a single character.

Why do you need atoi in the first place? It's a function from C for converting strings like atoi("532532").

If you want to convert a single char to int you can just do it like this.

const int number = c - '0';
Kaldrr
  • 2,780
  • 8
  • 11