Why does my while loop end early?

The trouble loop:

1
2
3
4
5
6
7
8
9
10
11
    while( anyShipOnGrid(wanderShip) )
    {
        cout << "\n\nPress enter to start next phase, moving each ship 50 units." << endl;
        cin.ignore();
        newTurn( wanderShip );
        for( int i=0; i < 10; i++ )
        {
            if( wanderShip[i].x_coord > 1024 || wanderShip[i].y_coord > 768 ){ wanderShip[i].onGrid=false; }
        }
        showGrid( wanderShip );
    }


Loop works fine with this condition check:

1
2
3
4
5
6
7
bool anyShipOnGrid(SpaceShip wanderShip[10])
{
    for( int i=0; i < 10; i++)
    {
        if( wanderShip[i].onGrid==true ){ return true; }
    }
}


But this one causes the loop to break at seemingly random times.

1
2
3
4
5
6
7
bool anyShipOnGrid(SpaceShip wanderShip[10])
{
    for( int i=0; i < 10; i++)
    {
        return wanderShip[i].onGrid==true;
    }
}


Please help me understand why.



Complete code:

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

struct SpaceShip
{
    int x_coord;
    int y_coord;
    int name;
    bool onGrid;
};

int randRange( int low, int high );
SpaceShip getNewShip(SpaceShip ship);
SpaceShip newTurn(SpaceShip wanderShip[10]);
void showGrid(SpaceShip wanderShip[10]);
bool anyShipOnGrid(SpaceShip wanderShip[10]);
void populateGrid( SpaceShip wanderShip[10] );
void shipProg();

int randRange( int low, int high )
{
    return rand() % ( high - low + 1 ) + low;
}
SpaceShip getNewShip(SpaceShip ship)
{
    ship.x_coord = randRange(1,1024);
    ship.y_coord = randRange(1,768);
    ship.onGrid=true;
    return ship;
}
SpaceShip newTurn(SpaceShip wanderShip[10])
{
    int x_movement;
    int y_movement;
    for( int i = 0; i < 10; i++ )
    {
        x_movement = ( randRange(1,50) );
        y_movement = 50 - x_movement;
        wanderShip[i].x_coord += x_movement;
        wanderShip[i].y_coord += y_movement;
    }
}
void showGrid(SpaceShip wanderShip[10])
{
    for( int i=0; i < 10; i++ )
    {
        if( wanderShip[i].onGrid==false ){ cout << "\nShip " << wanderShip[i].name << " is off the grid." <<  endl; }
        else
        {
            cout << "\nShip " << wanderShip[i].name << " location:  ( "
                 << wanderShip[i].x_coord << ", " << wanderShip[i].y_coord << " )" << endl;
        }
    }
}
bool anyShipOnGrid(SpaceShip wanderShip[10])
{
    for( int i=0; i < 10; i++)
    {
        //return wanderShip[i].onGrid==true;
        if( wanderShip[i].onGrid==true ){ return true; }
    }
}
void populateGrid( SpaceShip wanderShip[10] )
{
    for( int i=0; i < 10; i++ )
    {
        SpaceShip ship;
        ship=getNewShip(ship);
        ship.name=i+1;
        wanderShip[i]=ship;
    }
}
void shipProg()
{
    cout << "Ships have been generated. Their coordinates: " << endl;
    SpaceShip wanderShip[10];
    populateGrid(wanderShip);
    showGrid( wanderShip );
    while( anyShipOnGrid(wanderShip) )
    {
        cout << "\n\nPress enter to start next phase, moving each ship 50 units." << endl;
        cin.ignore();
        newTurn( wanderShip );
        for( int i=0; i < 10; i++ )
        {
            if( wanderShip[i].x_coord > 1024 || wanderShip[i].y_coord > 768 ){ wanderShip[i].onGrid=false; }
        }
        showGrid( wanderShip );
    }
    cout << "\n\nAll ships are off the grid.\n" << endl;
}

int main()
{
    srand( time( NULL ) );
    cout << "This program generates 10 ships and randomly positions them on a grid.\n"
            "The grid size is 1024 units across (x-axis) and 768 units high (y-axis).\n"
            "Ships always move in a positive direction, away from the point of origin ( 0,0 ).\n"
            "Every ship moves 50 units per phase.\n"
            "Movement across the x-axis is calculated first by \n"
            "generating a random number between 1 and 50.\n"
            "Any remaining amount is applied to movement up the y-axis.\n"
            "Ships are tracked and their positions reported until they move off the grid." << endl;

    char again='y';

    do
    {
        cout << "Press enter to generate ships." << endl;
        cin.ignore();
        shipProg();
        cout << "Run the program again?" << endl;
        cin >> again;
    }while( again!='n' );
}
Last edited on
But this causes the loop to break at seemingly random times.
Because that code snippet tests only first ship.

Lets unroll the loop:
1
2
3
4
5
6
7
8
9
10
11
return wanderShip[0].onGrid==true; //Execution won't get past this line
//↓ Never executed as we returned early
return wanderShip[1].onGrid==true;
return wanderShip[2].onGrid==true;
return wanderShip[3].onGrid==true;
return wanderShip[4].onGrid==true;
return wanderShip[5].onGrid==true;
return wanderShip[6].onGrid==true;
return wanderShip[7].onGrid==true;
return wanderShip[8].onGrid==true;
return wanderShip[9].onGrid==true;
I'm an idiot. Thank you.
Topic archived. No new replies allowed.