# Help with Structures

This is a discussion on Help with Structures within the C++ Programming forums, part of the General Programming Boards category; **Cue usual spiel about being a noob and new to programming** Here is the problem: Create an array of space ...

1. ## Help with Structures

**Cue usual spiel about being a noob and new to programming**

Here is the problem: 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 768pixels.

Build errors are at the bottom of the code...

I'm sure this is something obvious but I cannot figure out what I need to do here... Any hints or help would be appreciated.

Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

struct spaceShip
{
int x_coord;
int y_coord;
};
spaceShip ships[20];

spaceShip createShips (spaceShip ships[], int size)
{
for (int i = 0; i < size; i++)
{
ships[i].x_coord = rand() % 1024;
ships[i].y_coord = rand() % 768;
}
}
spaceShip updateLoc (spaceShip ships[], int size)
{
for (int i = 0; i < size; i++)
{
ships[i].x_coord = ships[i].x_coord + rand() % 100;
ships[i].y_coord = ships[i].y_coord + rand() % 50;
}
}
void displayTest (spaceShip ships[], int size)
{
cout << "{";
for (int i = 0; i < size; i++)
{
if ( i != 0 )
{
cout << "\n";
}
cout << ships[i].x_coord;
}
cout << "}";
}
spaceShip findHighest (spaceShip ships[], int size)
{
int highestValue = 0;
for (int i = 0; i < size; i++)
{
if (spaceShip ships[i] > highestValue)
{
highestValue = spaceShip ships[i];
}
}
return highestValue;

int main ()
{
srand(time(NULL));
createShips(ships, 20);
displayTest(ships, 20);
findHighest(ships, 20);
while (highestValue < 1024)
{
updateLoc(ships, 2)
findHighest(ships, 20);
}
cout << "All Ships have left the screen!\n";
}

C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip createShips(spaceShip*, int)':|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|23|warning: no return statement in function returning non-void|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip updateLoc(spaceShip*, int)':|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|31|warning: no return statement in function returning non-void|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip findHighest(spaceShip*, int)':|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|50|error: expected primary-expression before 'ships'|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|50|error: expected ')' before 'ships'|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|52|error: expected primary-expression before 'ships'|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|52|error: expected ';' before 'ships'|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|55|error: conversion from 'int' to non-scalar type 'spaceShip' requested|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|58|error: a function-definition is not allowed here before '{' token|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|69|error: expected '}' at end of input|
||=== Build finished: 7 errors, 2 warnings ===|```

2. First count your braces, does every starting brace have a matching ending brace?

Jim

3. Code:
```spaceShip createShips (spaceShip ships[], int size)
{
for (int i = 0; i < size; i++)
{
ships[i].x_coord = rand() % 1024;
ships[i].y_coord = rand() % 768;
}
// C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip createShips(spaceShip*, int)':|
// C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|23|warning: no return statement in function returning non-void|
// It's complaining that you don't have
// return something;
}```
But since you're
a) just writing the results into the supplied array
b) you call the function AND ignore the result
then perhaps you should just make it a void function.

4. Ok I feel like an idiot, found the missing brace but it seems to have made it worse.... Now it says I am missing braces and semicolons everywhere but I cant find any missing...

Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

struct spaceShip
{
int x_coord;
int y_coord;
};
spaceShip ships[20];
int highestValue;

spaceShip createShips (spaceShip ships[], int size)
{
for (int i = 0; i < size; i++)
{
ships[i].x_coord = rand() % 1024;
ships[i].y_coord = rand() % 768;
}
}
spaceShip updateLoc (spaceShip ships[], int size)
{
for (int i = 0; i < size; i++)
{
ships[i].x_coord = ships[i].x_coord + rand() % 100;
ships[i].y_coord = ships[i].y_coord + rand() % 50;
}
}
void displayTest (spaceShip ships[], int size)
{
cout << "{";
for (int i = 0; i < size; i++)
{
if ( i != 0 )
{
cout << "\n";
}
cout << ships[i].x_coord;
}
cout << "}";
}
spaceShip findHighest (spaceShip ships[], int size)
{
highestValue = 0;
for (int i = 0; i < size; i++)
{
if (spaceShip ships[i] > highestValue)
{
highestValue = spaceShip ships[i];
}
}
return highestValue;
}

int main ()
{
srand(time(NULL));
createShips(ships, 20);
displayTest(ships, 20);
findHighest(ships, 20);
while (highestValue < 1024)
{
updateLoc(ships, 2);
findHighest(ships, 20);
}
cout << "All Ships have left the screen!\n";
}

C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip createShips(spaceShip*, int)':|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|23|warning: no return statement in function returning non-void|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip updateLoc(spaceShip*, int)':|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|31|warning: no return statement in function returning non-void|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip findHighest(spaceShip*, int)':|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|50|error: expected primary-expression before 'ships'|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|50|error: expected ')' before 'ships'|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|52|error: expected primary-expression before 'ships'|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|52|error: expected ';' before 'ships'|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|55|error: conversion from 'int' to non-scalar type 'spaceShip' requested|
||=== Build finished: 5 errors, 2 warnings ===|```

5. I'm not quite sure what you mean by ignore the results, I use the results to see where the ships are laid initially right? I am lost

6. Look at this
ships[i].x_coord = ships[i].x_coord + rand() % 100;

Now look at this
if (spaceShip ships[i] > highestValue)

It seems you copied the whole type and variable name from the function parameter declaration.

> I'm not quite sure what you mean by ignore the results, I use the results to see where the ships are laid initially right? I am lost
Code:
```int good ( ) {
return 42;
}
return 1234;
}
int ugly ( ) {
// now what is returned here? garbage? worse?
}

int main ( ) {
int a = good();
bad();  // now why exactly did you return 1234?
ugly(); // garbage returned, but ignored anyway (we hope)
}```

7. sorry I am new to this and am not quite sure why I need to have a result output in that function... and if I do need one then what should it be? I tried making it a void and it didnt really change anything program still doesn't compile.

forgive me so would correct syntax be

if (ships[i].x_coord > highestValue) ?

8. Ok so I changed my if statement in the highestValue function and now just getting the return errors that I just don't understand...

Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

struct spaceShip
{
int x_coord;
int y_coord;
};
spaceShip ships[20];
int highestValue;

spaceShip createShips (spaceShip ships[], int size)
{
for (int i = 0; i < size; i++)
{
ships[i].x_coord = rand() % 1024;
ships[i].y_coord = rand() % 768;
}
}
spaceShip updateLoc (spaceShip ships[], int size)
{
for (int i = 0; i < size; i++)
{
ships[i].x_coord = ships[i].x_coord + rand() % 100;
ships[i].y_coord = ships[i].y_coord + rand() % 50;
}
}
void displayTest (spaceShip ships[], int size)
{
cout << "{";
for (int i = 0; i < size; i++)
{
if ( i != 0 )
{
cout << "\n";
}
cout << ships[i].x_coord;
}
cout << "}";
}
spaceShip findHighest (spaceShip ships[], int size)
{
highestValue = 0;
for (int i = 0; i < size; i++)
{
if (ships[i].x_coord > highestValue)
{
highestValue = ships[i].x_coord;
}
}
return highestValue;
}

int main ()
{
srand(time(NULL));
createShips(ships, 20);
displayTest(ships, 20);
findHighest(ships, 20);
while (highestValue < 1024)
{
updateLoc(ships, 2);
findHighest(ships, 20);
}
cout << "All Ships have left the screen!\n";
}

C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip createShips(spaceShip*, int)':|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|23|warning: no return statement in function returning non-void|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip updateLoc(spaceShip*, int)':|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|31|warning: no return statement in function returning non-void|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp||In function 'spaceShip findHighest(spaceShip*, int)':|
C:\Users\Jake\Desktop\C++\Jumping_Into_C++\TBD\main.cpp|55|error: conversion from 'int' to non-scalar type 'spaceShip' requested|
||=== Build finished: 1 errors, 2 warnings ===|```

9. No, but seriously, do you understand how functions work? Do you understand how to return values from a function? Do you understand how to specify return types for functions?

10. Elysia, actually I know how to return values but honestly have no idea what you mean by return types...(**edit :this must have something to do with why I had to change the spaceShip lowestValue to int lowestValue because the return was an int and spaceShip isn't an int? and they have to match?) You were right Salem, voids were better since the values weren't really being used elsewhere, I did have an error left with spaceShip findHighest, apparently I can't do that since I have it return highestValue which is an int and spaceShip is a non-scalar? Didn't know that one but I changed it to the below function and it now compiles fine and works... Also figured out what I really wanted was the lowest value not the highest so I fixed that as well, now it does exactly what I wanted... Sorry for my stupidity guys and thanks for bearing with me as I am still very much a beginner...

Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

struct spaceShip
{
int x_coord;
int y_coord;
};
spaceShip ships[20];
int lowestValue;

void createShips (spaceShip ships[], int size)
{
for (int i = 0; i < size; i++)
{
ships[i].x_coord = rand() % 1024;
ships[i].y_coord = rand() % 768;
}

}
void updateLoc (spaceShip ships[], int size)
{
for (int i = 0; i < size; i++)
{
ships[i].x_coord = ships[i].x_coord + rand() % 100;
ships[i].y_coord = ships[i].y_coord + rand() % 50;
}

}
void displayTest (spaceShip ships[], int size)
{
cout << "{";
for (int i = 0; i < size; i++)
{
if ( i != 0 )
{
cout << "\n";
}
cout << ships[i].x_coord;
}
cout << "}";
}
int findLowest (spaceShip ships[], int size)
{
lowestValue = 1024;
for (int i = 0; i < size; i++)
{
if (ships[i].x_coord < lowestValue)
{
lowestValue = ships[i].x_coord;
}
}
return lowestValue;
}

int main ()
{
srand(time(NULL));
createShips(ships, 20);
displayTest(ships, 20);
findLowest(ships, 20);
while (lowestValue < 1024)
{
updateLoc(ships, 20);
findLowest(ships, 20);
displayTest(ships, 20);
}
cout << "All Ships have left the screen!\n";
}```

11. honestly have no idea what you mean by return types...(**edit :this must have something to do with why I had to change the spaceShip lowestValue to int lowestValue because the return was an int and spaceShip isn't an int? and they have to match?)
If you don't know what a function return type is, you should probably review the following links: Functions I and Functions II.

Jim