Source Code

robo.h - http://pastie.org/3698136
main.cpp - http://pastie.org/3698145
functions.cpp - http://pastie.org/3698148

Glitches

When ever i try to create more than 2 instances of the class Robots the program will compile but crash
it is crashing during construction right after the first while loop, i would think it was the validMove function but i don't see any errors.

Recommended Answers

All 5 Replies

Welcome to Daniweb. Many of the routine people who respond won't download code from the web so it's best to post it here.

Use your debugger or toss in output statements to keep track of variable values as you go through the program to try to track down where the problem is. Learning how to use these run time debugging techniques are part of the process of becoming a reasonable programmer.

Your random generation function is wrong. First, you should not seed the random number generator every time you query for a number. You should only seed it once at the start of the main() function.

Also, your range computation in it is also wrong. You had this:

double ranDec(int start, int end, bool floater) {//by defult will return value between 0 and 1000
    srand (time(NULL)+rand());//creates random seed based off current time
    double num;
    if(!end) {//end=0 and or start=0
        num = rand()+start;
    } else {//no 0's passed or start=0
        num = (rand() % end + start);
    }
    if(floater) {
        num = num + (rand()/(static_cast<double>(RAND_MAX) + 1.0));
    }
    return num;
}

But you should have this:

double ranDec(int start, int end, bool floater) {//by defult will return value between 0 and 1000
    double num;
    if(end == start) { 
        num = rand() % 1000 + start;   // default of 1000 in range.
    } else {
        num = (rand() % (end - start) + start);  // this is correct.
    }
    if(floater) {  // ??? what is this?
        num = num + (rand()/(static_cast<double>(RAND_MAX) + 1.0));
    }
    return num;
}

Also, in your robot constructor, you should call the random numbers as so:

        this->x = (int)ranDec(1,screenX-2,false);  // this will always generate an in-bound value.
        this->y = (int)ranDec(1,screenY-2,false);

Other than that, I don't know what the crash is about, maybe you should investigate further (print out values of the variables around the point of crash) and identify the exact point of the crash. And you should also post the actual error that you get, not just "it crashes", because a program can crash in different ways and for different reasons, that we'd like to know in order to help you.

Thank you for your feed back
This is the first time I haven't been able to correct my own problems,

There was no compiler errors, the program would just crash, I changed the directionX, directionY, x,y all from int to short and now I have been able to create multiple "Robots"

also

     if(floater) {//if set true then will return a random float compared to a random int

I don't want to do this
because i plan to expand the speed later, also explore allowing diagonals

    this->x = (int)ranDec(1,screenX-2,false);  // this will always generate an in-bound value.
    this->y = (int)ranDec(1,screenY-2,false);

If you have a lot of objects/variables at one time you may be overloading the stack. I say that because I would expect that using short instead of int would save a little room on the stack. Moving some of the objects to the heap instead of keeping them in static memory on the stack may allow you to make more objects without overloading the stack and crashing a program that compiles at run time.

how would I

Moving some of the objects to the heap

I have limited understanding of stack and heap

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.