-3
#include <iostream>
#include <Windows.h>
#include <time.h>
#include <stdlib.h>
#include <cmath>
#include <cstdlib>

using namespace std;

//Screen dimensions
const int width = 800, height = 600;

//pixels deimensions
const int dw = 8, dh = 16;
//800/8 100 and 600/16=37.5
// Ü  Û ß

struct coord //Structures to strogare Dinosaurs characters
{
    int x;
    int y;
};

/*/struct part
{
    coord peri[93];
    char D[93];
};

part car1[2];
*/

void gotoxy(int x, int y) {
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD dwPos;
    dwPos.X = x;
    dwPos.Y = y;
    SetConsoleCursorPosition(hcon, dwPos);
}

void convert(int P[2], float x, float y) //Take the integer part from any value
{
    P[0] = int(x) / dw;
    P[1] = int(y) / dh;
}

void convert(int P[2], int x, int y) //Take the integer part from any integer
{
    P[0] = x / dw;
    P[1] = y / dh;
}

void drawpoint(char plane[height / dh][width / dw + 1], int A, int B, char C)//Function to draw the screen
{
    B = height / dh - 1 - B;
    if (A < 0 || B < 0 || A >= width / dw || B >= height / dh)  return;
    plane[B][A] = C;
}

class map
{
private:
    float n(float x)
    {
        return 0 ;
    }
public:
    float f(float x)
    {
        return (150 * (n(x) + 1.0f) / 2.0f); //75
    }

    void draw(char plane[height / dh][width / dw + 1])
    {
        for (int i = 0; i < width / dw; i++)
        {
            int P[2];
            // 75/16= 4.68

            int j_start = f(i * dw) / dh;
            for (int j = j_start; j >= 0; j--)
            {
                convert(P, i * dw, j * dh);
                drawpoint(plane, P[0], P[1], 'o');
            }
            convert(P, i * dw, j_start * dh);
            drawpoint(plane, P[0], P[1], '#');
        }
    }
};

int main()
{
    map new_map;
    char plane[height / dh][width / dw + 1]; //High  37 width 100+1;
    for (int i = 0; i < height / dh; i++) {
        for (int j = 0; j < width / dw; j++) {
            plane[i][width / dw] = '\n'; //when plane width is 100, line break
        }
       
        plane[height / dh + 1][width / dw] = '\0';  //Define the end of the heigh
    }

    while (true)
    {
        gotoxy(0, 0);
        for (int i = 0; i < (height / dh); i++)
        {
            for (int j = 0; j < (width / dw); j++)
            {
                plane[i][j] = '*';
            }
        }
        new_map.draw(plane);
        puts(plane[0]);
    }
    return 0;
}

The map should look like this:

"***********************
"***********************
"***********************
"***********************
"***********************
"***********************
"***********************
"***********************
####################################################################
ooooooooooooooooooooooo
ooooooooooooooooooooooo
ooooooooooooooooooooooo

But I get something like this instead:

"***********************
"***********************
"***********************
"***********************
"***********************
"***********************
"***********************
"***********************
#####################################################################
oooooooooooooooooooooooo
oooooooooooooooooooooooo
oooooooooooooooooooooooo
oo╠╠╠╠╠╠╠╠%
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
jjuanda19
  • 1
  • 1
  • 6
    `using namespace std;` and naming your own type `map` is confusion. [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – 463035818_is_not_an_ai Mar 01 '23 at 17:01
  • 3
    why are you not using `std::array` or `std::vector`? or `std::string` for strings? – 463035818_is_not_an_ai Mar 01 '23 at 17:04
  • And why are you using `puts`, a prehistoric C library function? – Sam Varshavchik Mar 01 '23 at 17:05
  • `void draw(char plane[height / dh][width / dw + 1])` may not be doing what you think it's doing. But also, why hazard using C-style arrays instead of a C++'s much better behaved containers? – Brian61354270 Mar 01 '23 at 17:05
  • 3
    Crank up the compiler warnings and you might see something like *:105:9: warning: array index 38 is past the end of the array (which contains 37 elements) [-Warray-bounds]* I built with g++ and the compiler options `-O3 -Wall -Wextra -pedantic -fsanitize=address,undefined`. – user4581301 Mar 01 '23 at 17:09
  • 1
    `plane[height / dh + 1][width / dw] = '\0'` in a `char plane[height / dh][...]` you are two rows past the end of your array, last valid index is `[height / dh - 1]`. – teapot418 Mar 01 '23 at 17:10
  • Initilize `plane` like this: `char plane[height / dh][width / dw + 1] = {0};` – selbie Mar 01 '23 at 17:24
  • `╠` is a result of debug mode of your compiler trying to tell you that you reading uninitialized memory. `╠` is 0xCC : [https://stackoverflow.com/questions/127386/what-are-the-debug-memory-fill-patterns-in-visual-studio-c-and-windows](https://stackoverflow.com/questions/127386/what-are-the-debug-memory-fill-patterns-in-visual-studio-c-and-windows) – drescherjm Mar 01 '23 at 17:36

1 Answers1

2

You have traversed past the end of your buffer and are printing random memory contents.

Consider using std::vector or some other STL container to get built-in range checking with range-based for loops.

Mikel F
  • 3,567
  • 1
  • 21
  • 33