Spaceship coordinates - structures

Hi guys,

Im having trouble with the while loop, its exiting the loop if either x or y is above the specified limit, and not both.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  #include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>

using namespace std;


///Create an array of space ship objects and write a program that continually updates their
//positions until they all go off the screen. Assume that the size of the screen is 1024 pixels by 768
//pixels.

struct spaceShip
{
	string name;
	int xcoordinates;
	int ycoordinates;

};


int main()
{
	srand(time(0));

	spaceShip red;
	red.name = "Red";
	red.xcoordinates = 0;
	red.ycoordinates = 0;


	spaceShip green;
	green.name = "Green";
	green.xcoordinates = 0;
	green.ycoordinates = 0;




	do{

		red.xcoordinates += rand()%150;
		red.ycoordinates += rand()%100;
		cout << red.name << " " << red.xcoordinates << " by " << red.ycoordinates << endl;

		green.xcoordinates += rand()%150;
		green.ycoordinates += rand()%100;
		cout << green.name << " " << green.xcoordinates << " by " << green.ycoordinates << endl;

	}while((red.xcoordinates < 1024 && red.ycoordinates < 768) || (green.xcoordinates < 1024 && green.ycoordinates < 768));

	if(red.xcoordinates >= 1024 && red.ycoordinates >= 768)
	{
		cout << red.name << " is at position: " << red.xcoordinates << " by " << red.ycoordinates << " and is off the screen." << endl;
	}else if(green.xcoordinates >= 1024 && green.ycoordinates >= 768)
	{
		cout << green.name << " is at position: " << green.xcoordinates << " by " << green.ycoordinates << " and is off the screen. " << endl;
	}




	


	system("pause");
}
Topic archived. No new replies allowed.