LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2005, 08:30 PM   #1
mshinska
LQ Newbie
 
Registered: Oct 2005
Posts: 14

Rep: Reputation: 0
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;

}
 
Old 10-08-2005, 10:05 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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

Last edited by paulsm4; 10-08-2005 at 10:07 PM.
 
Old 10-08-2005, 10:18 PM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
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 {
    STATEMENTS
} while(CONDITIONAL);
Also, just by glancing over it, you're doing some weird stuff with boolean ANDs. I don't really want to assume anything, but I don't think you want to be using "&&" like that. Maybe you do, but in either case, it sure makes for hard-to-read code.

Sorry if this doesn't help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble using libwnck in my program. rob_of_ownsboro Programming 1 05-03-2005 04:25 PM
C++ - Wondering why my program wont loop? eclipse75 Programming 2 03-27-2005 09:16 PM
Binutils 2.14 loop trouble EPM_Students Linux From Scratch 6 01-02-2005 11:30 PM
Binutils 2.14 loop trouble (different issue) VillageIdiot Linux From Scratch 5 12-26-2004 03:41 PM
Program on boot-up won't stop running in a continuous loop... CanadianSlacker Slackware 6 01-14-2004 01:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration