Hello daniweb peeps, been learning c++ from a beginners book for the past week or two but have got myself a bit confused, follows is the code that compiles but isn't doing what i want it to (Im hoping the fault is obvious!)

#include <iostream>

using namespace std;

int main(){




int toX = 100, toY = 200, currentX = 0, currentY = 0;

do{

if(currentX < toX)
{ currentX++; }
else
{ currentX--; }

if(currentY < toY)
{ currentY++; }
else
{ currentY--; }

cout << "Scout curr location: " << currentX << "," << currentY << endl;


} while ( (toX != currentX) && (toY != currentY) );

cout << "Unit arrived at " << toX << "," << toY << endl;


return 0;

}

My issue is, the while statement is obviously becoming true and ending my loop too early, it will say "scout arrived at 100,200" - it will of jumped, it seems to be that the while statement is not looking at the && section and is just using part of it..

Remember to word your replies if there are any in easy terms,
thanks for any input you may have!
cheers
Paul

(i did use search etc but nothing of any relevence to my basic question!)

it will say "scout arrived at 100,200"

After reviewing your loop, this seems to be the correct output in order to establish the terminating condition of your loop.. where toX == currentX AND toY == currentY... which will occur when toX becomes 100 (which is the initialized value of currentX) and toY becomes 200 (which is the initialized value of currentY)

replace the && with the ||. You wan't the loop to run if either currentX OR currentY is not equal to toX or toY, respectively.

Also take out the

else { currentX-- } and else{ currentY-- }

there is not point since say currentX will not increment if its 100.

commented: very good advise +0

Thanks for both of your input i can see why you would say that..

However, i think i should of explained a bit better.

A unit will be able to travel to - (minus) grid co-ords not just positive, so the 'useless' code would allow a unit to travel to -200,-100 for example.

If you just pop that code into your favourite compiler and build it, you'll see that the X and Y current coords (as outputted using cout), they go up together in each loop, but once the X coord reaches toX the loop/do while loop breaks out - which isn't what i want.

I want (for ex) the Y co-ord to carry on incrementing but have the X co-ord stay as it is.
Here is a concrete example of this:

(x,y (output):

toX = 21
toY = 10...

1,1
2,2
3,3
4,4
5,5
6,6
7,7
8,8
9,9
10,10
11,10
12,10
13,10
14,10
15,10
16,10
17,10
18,10
19,10
20,10
21,10

Am i making any sense? - struggling to work out how to do this, i considered - for- loops but i dont think this will achieve what i want any better..

Any other ideas?
cheers
Paul

Ah showing my true newbieness!

Taken the last advise posted and used OR instead of AND and it works, doesn't make sense to me right now but will have to mull it over for a bit!

cheers!

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.