having trouble with the do while loop in program
my program compiles but doesn't ever move past the do/ while loop.
#include <iostream> #include <cstdlib> #include <cmath> using namespace std; int main(void) { int atHome=0, stepsToTry=5000, home, start, steps=0; double x, y, dx, dy, angle; const int minx = -10; const int maxx = 10; const int miny = -10; const int maxy = 10; const double PI = 4*atan(1.0); cout << "Will Waldo make it home? \n\n"; cout << "Input an int to start the random number generator: "; int myStart; cin >> myStart; srand(myStart); home = (((x>=8) && (x<=10)) && ((y>=8) && (y<=10))); start = ((x = 0) && (y = 0)); for (stepsToTry = 5000; stepsToTry>0; stepsToTry = stepsToTry - 1) { do { steps = steps + 1; angle = 2*PI*rand()/RAND_MAX; dx = cos(angle); dy = sin(angle); } while (((dx>=-10) && (dx<=10)) && ((dy>=-10) && (dy<=10))); { start = (dx && dy); if (start == home) { atHome=1; } } } if (atHome=1) { cout<< "Waldo made it home in "<<steps<<"! \n\n"; } else if (steps = 5000) { cout<<"Waldo is still lost after taking 5000 steps \n\n"; } return 0; } |
Two questions:
1. What kind of debugger do you have available to step through and see if dx and dy are being incremented the way you think they are? (Hint: will they EVER be incremented to a value that will cause the loop to exit)? 2. Could you please familiarize yourself with the LQ "[code]" tags, to help make your code more legible? Thanx in advance .. PSM |
do {...} while(...);
I think there might be a problem with the way you've constructed your do/while loop; you have a code block underneath the while statement that doesn't do anything except change the scope of "start". It looks like you might have intended the block to be part of a while loop, but do/while works like this:
Code:
do { Sorry if this doesn't help. |
All times are GMT -5. The time now is 09:48 PM. |