Help with arrays

Hello all, firstly thanks for any attempts to help me solve my problems, I appreciate any advice!

Currently I have a task at hand which I am really struggling with.

3 docks, 10 rows with 10 spaces in each row so 100 spaces in each dock, making 300 spaces altogether.

When I start my code the user will input the name and size of the ship, where then i store the name and size as a string variable.

I am really struggling trying to put the variable into an array
For example : int berth[100];

Any advice that i could put the name of the ship into berth[i] but only if the ship is the correct size and if there is spaces left.

Thanks for any advice
Last edited on
Bump
you cannot store a string into int array

You need to break the problem down into smaller chunks. I would create some structs or classes (but you probably don't know about classes yet), like so:

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
enum shipsize
{
	small = 0,
	medium,
	large
};

struct ship
{
	char* name;
	shipsize sz;
};

struct row
{
	ship small_spaces[5];
	ship medium_spaces[3];
	ship large_spaces[2];
};

struct berth
{
	row rows[10];
};

struct dock
{
	berth berths[10];
};

struct port
{
	dock docks[3];
};


int main(int argc, char* argv[])
{
	port myport;

	ship myfirstship = {"First small ship", small};
	myport.docks[0].berths[0].rows[0].small_spaces[0] = myfirstship;

	return 0;
}


You would then add some code to the port to add a ship which then determines the location for that size ship to berth etc...

HTH
ooo Thanks for the advice... We have started the classes but i'm just trying to break it down bit by bit without over complicating myself, will have a look at this right away thanks

EDIT: Now you have given me an example of enums and structs i get this alot more than before thank you
Last edited on
Topic archived. No new replies allowed.