mercredi 4 mars 2020

C++ ints getting corrupted, generated with rand()

I'm attempting to write a C++ program that uses a Point class consisting of x and y coordinates (ints) and a boolean. The x and y coordinates should be in the range from 0-100, but upon execution and with a print statement to test, the values printed out are corrupted.

My code looks like this:

int main(int argc, const char * argv[]) {
vector<Point> points;

for (int i = 0; i<200; i++) {
    int rand_X = (rand() % 100) + 1;
    int rand_Y = (rand() & 100) + 1;

    Point point = Point(rand_X, rand_Y, false);
    cout<< point.getX() << " " << point.getY() << endl;
    points.push_back(point);
}

return 0;

and the Point class looks like this, if that's relevant:

struct Point { 

public:

int x;

int y;

bool in_layer;

Point(int x, int y, bool in_layer) {}
int getX() {return this->x;}
int getY() {return this->y;}

};

And the output is just "-272632592 32766" repeated 200 times, which I'm assuming is a corrupted value.

Any ideas what could be causing the issue?




Aucun commentaire:

Enregistrer un commentaire