PDA

Click to See Complete Forum and Search --> : not plotting correctly...


SteveCRM
Jan 3rd, 2001, 09:17 PM
why don't these plot correctly? in bold is the important code:

/*---=== C++ BALL MOVER===---
---===STEVE MACK===---*/

#include <iostream.h> //our normal i/o header

int dummy; //so the console doesn't close too early
int Y; //THE Y AXIS IN THE MATRIX
int X; //THE X AXIS IN THE MATRIX
int ballY; //The ball's Y value
int ballX; //The ball's X value
int mainLoop; //Used to call the code so many times

int plotBall(int PlotX, int PlotY); //plot function prototype
int clearScreen(); //clearScreen function prototype

int main() //the main function
{
cout<<"|||---===Welcome to BALL MOVER v0.1===---|||"<<endl; // Title stuff
cout<<"BALL MOVER was written by Steve Mack"<<endl; // author info

ballY=1; // } These 2 vars will hold the balls position
ballX=1; // }

for(mainLoop=1; mainLoop<6; mainLoop++){
clearScreen(); //this will clear our screen
plotBall(ballX,ballY);
ballX=ballX++;
ballY=ballY++;
}

cout<<endl<<"Enter Anything To Quit: "; // } This is the ending sequence
cin>>dummy;// } "Press any key to exit", KEYPRESSED, close;
return 0; // }
}


//================*************FUNCTIONS*************=====================

int plotBall(int PlotX, int PlotY){ //this function plots the ball
//THE MAIN MATRIX
int FinalDrawVar;

for(X=1; X<82; X++){ //Goes through the X axis
if(PlotX==X){
for(Y=1; Y<11; Y++){
cout<<endl; //next line
if(PlotY==Y){
for(FinalDrawVar=1; FinalDrawVar<(X--);FinalDrawVar++){
cout<<"*"; //draw the spaces
}//End FinalDrawVar loop
cout<<"O"; //draw the ball
X=NULL;
Y=NULL;
return 0;
}

}//end Y Loop
} //ends drawing the ball
} //end X axis cycle
return 0;
}

int clearScreen(){ // This function will clear the screen.
//couts 5041 spaces to clear the screen
int clearScreenCounter;
for(clearScreenCounter=1; clearScreenCounter<11; clearScreenCounter++){ //endl does what we need
cout<<endl;
}
return 0;
}

HarryW
Jan 3rd, 2001, 10:05 PM
I think your problem is in this code:
for(FinalDrawVar=1; FinalDrawVar<(X--);FinalDrawVar++){
cout<<"*"; //draw the spaces
}//End FinalDrawVar loop


X-- is a statement. It decrements (takes one away from) X. Every time the for loop finishes, it checks if the expression ( FinalDrawVar<(X--) ) evaluates to true, and if it does it repeats the loop. Every time this expression is evaluated, it decrements X.

So all you have to do is change X-- to X-1, and it should work.

HarryW
Jan 3rd, 2001, 10:11 PM
Oh yes, by the way, it's a bit pointless writing BallX = BallX++. You may as well just write BallX++.

++X is preincrement
--X is predecrement
X++ is postincrement
C++ is postdecrement

The difference between pre and post is that ++X evaluates to X+1 as an expression, and X++ evaluates to X as an expression. Both act the same as a statement though - basically they represent X=X+1.

SteveCRM
Jan 4th, 2001, 02:12 PM
But 'X' is cycling though my box...keeping it at 1 only checks the first column...I probably should have stated the problem better :rolleyes: If I have it plot at 6,1 and loop it 4 times, it comes out something like
First time: 5.1
Second Time: 5,1
Third Time: 6,1
Fourth Time: 6,1
Fifth Time 7,1

:confused:

HarryW
Jan 4th, 2001, 03:44 PM
Right.... and what is it doing at the moment?

SteveCRM
Jan 4th, 2001, 08:03 PM
I put the carot(^) in so I could count spaces before the "O" is drawn, but it doesn't count the amount of spaces I specify. :(