Segmentation fault

Hi, I have a project where I am required to make a working 2-player game of battleship. My code is written up until players actually start making moves but I'm getting a core dump.

I'm thinking the problem is in my arrays shipType, loc, and size. However, I don't know whether it lies in my main or the function they are declared in. If someone more experienced with such matters could examine my code and get back to me I'd be very happy!


#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

using namespace std;

const int rows = 10;
const int columns = 10;
const int EachShip = 5;


struct players{
  string name, ships[EachShip];
  int size[EachShip];
  char board[rows][columns];
  int loc[EachShip];
} play1, play2;



int initialization(players &play1, players &play2);
void aBoard(players &player);
void initarr(players &play1, players &play2, string, int, int);
void input1(players &player);
void placement(players &player, int, int, int, int);
int currentBoard(players &player);

int initialization(players &play1, players &play2) {

play1.name = "Player 1";
play2.name = "Player 2";

string shipType[] = {"Carrier, Battleship, Destroyer, Submarine, Patrol Boat"};
int loc[] = {1, 1, 1, 1, 1, 1};
int size[] = {5, 4, 2, 3, 3};


return shipType[4], loc[4], size[4];

}

void initarr(players &play1, players &play2, string shipType[], int loc[], int size[]){

for (int i = 0; i < EachShip; i++)
   play1.ships[i] = shipType[i];
for (int i = 0; i < EachShip; i++)
   play1.loc[i] = loc[i];
for (int i = 0; i < EachShip; i++)
   play1.size[i] = size[i];
for (int i = 0; i < EachShip; i++)
   play2.ships[i] = shipType[i];
for (int i = 0; i < EachShip; i++)
   play2.loc[i] = loc[i];
for (int i = 0; i < EachShip; i++)
   play2.size[i] = size[i];
}

int main(int argc, const char *argv[]){
int play;
int loc[4], size[4];
string shipType[4];
cout << "Welcome to 2-player battleship. Press 1 to continue or anything else to quit." << endl;
cin >> play;
{
	if (play == 1){
	   	initialization(play1, play2);
   		initarr(play1, play2, shipType, loc, size);
		aBoard(play1);
		cout << endl << "------------------------------" << endl;
		aBoard(play2);
		system("clear");
		input1(play1);
		input1(play2);
}
	else
	{
		return 0;
	}
}
   return 0;
}

void aBoard(players &player){

cout << player.name << "'s initial board. " << endl << endl;

	for(int x = 0; x < rows; x++)
	{
		for(int y = 0; y < columns; y++)
		{
			player.board[x][y] = '0';
			cout << player.board[x][y] <<  " | ";
		}
		cout << endl;	
	}
}

void input1(players &player) {
int aShip, x, y, direction;
cout << player.name << endl;
for (aShip = 0; aShip < EachShip; aShip++)
{
	cout << "Place your " << player.ships[aShip] << ". To do so, first type the respective row (or x coordinate) of your choice. " << endl;
	cin >> x;
	if (x < 1 || x > 10){
	   cout << "Common... Follow the directions!" <<endl;
	}
	cout << "...and column(y coordinate)" <<endl;
	cin >> y;
	if (y < 1 || y > 10){
	   cout << "Common... Follow the directions!" << endl << endl;}
	cout << "Now, do you want the ship to be placed vertically (1) or horizontally (2)?  " <<endl;
	cin >> direction;
	cout <<endl;
	if (direction != 1 && direction != 2)
	   cout << "Common... Follow the directions! Now re-do your selections." << endl <<endl;
	else
	{
	placement(player, x, y, direction, aShip);
	currentBoard(player);
	}
}

}

void placement(players &player, int x, int y, int direction, int aShip){
system("clear");
if ((x < 1 || x > 10) || (y < 1 || y > 10) || (direction != 2 && direction != 1))
   input1(player);

   if (direction == 2) {
   	for (int d = 0; d < player.size[aShip]; d++) {
		player.board[x + d][y] = player.loc[aShip];
	
	}
   }
   else {
   	for (int d = 0; d < player.size[aShip]; d++){
	   player.board[x][y + d] = player.loc[aShip];
   
   }
}

}
int currentBoard(players &player){

cout << "  1 2 3 4 5 6 7 8 9 10" << endl;
for (int y = 0; y != 10; y++){
	if (y < 9)
	   cout << y + 1 << "  ";
	else
	   cout << 10 << "  ";
	for (int x = 0; x != 10; x++) {
		
	   	cout << player.board[x][y] << " ";
	}
	cout << endl;
    }
}
Line 164: You forgot to return value from your function;
Line 43: return shipType[4], loc[4], size[4]; is equivalent to return size[4];
And there is couple of shadowing (local variables play1 and play2 shadows global ones)
Ok so I updated my code so that the arrays aren't an issue (atleast I hope so). However, could you point out where I shadowed global variables? I'm not really sure where to look for them.
A simple run through debugger shows, that segmentation fault is on line 51:
1
2
3
4
void initarr(players &play1, players &play2, string shipType[], int loc[], int size[]){
for (int i = 0; i < EachShip; i++)
   play1.ships[i] = shipType[i];//← Here
for (int i = 0; i < EachShip; i++)
The problem is that you are trying to access your ship type array elements 0 to 4, but your shipType is declared as having only one element.

Shadowing is in functions initarr() and initialization(). Turn on your compiler warnings.
Topic archived. No new replies allowed.