I'm creating 7x10 array that filled 0's unless it is copied by ships.Currently I'm having a problem in which I'm not sure how to place the ships inside this grid.
this is how i've created the map. Please help as I'm a very beginner in c++. Thank you.

void displayBoard()
{

char board [rows][cols];
int num1=0;
int num2=0;
for (int i =1; i <= 7; ++i)
{
for (int j = 1; j <= 9; ++j)
{
cout << " ";
while (num1 <=9)
{
cout << " " << num1;
num1++;
}
}

while (num2 <=7)
{
cout << endl;
cout <<num2 << " | | | | | | | | | | |" << endl;
num2++;
cout << " ========================================…
}
} cout << board[rows][cols];
}

What you posted will work only inside that function. As soon as the function exits the grid will be destroyed. You can resolve that by passing a pointer to the grid to the function, something like this:

void displayBoard(char board[][10])
{
   // code goes here

 }


 int main()
 {
     char board[7][10] = {0};
     displayBoard(board);
}

Edited 4 Years Ago by Ancient Dragon

is there any other way to do it as im not good in pointer.Im a very beginner.

I've edited it and it works.But Im not sure whether its correct or not.Is that now I can place the ship inside the grid? if yes how should I do it? please help me

void new_game();
void load_game();
void displayBoard(char [][10]);

//Global variables
const int rows=7;
const int cols =10;
int coordinatep1[7][10],coordinatep2[7][10];
int placeship1[7][10],placeship2[7][10];


void new_game()
{

    string player1, player2, nameOfFile;
    cout <<" Enter name of player1:";
    cin >> player1;
    cout << "Enter name of player2:";
    cin >> player2;
    cout << "Enter the name of file to be saved: ";
    cin >> nameOfFile;
    system ("cls");

       char board[7][10] = {0};
        displayBoard(board);
        //displaymapenemy();

    system ("pause");
    //system("cls");
}

void load_game()
{
    string nameOfFile, P1password,P2password;
    cout << "Enter the name of file: ";
    cin >> nameOfFile;
    cout << "Enter player1 password: ";
    cin >> P1password;
    cout << "Enter player2 password: ";
    cin >> P2password;
}

void displayBoard(char board[][10])
{

    // char board [rows][cols];
     int num1=0;
     int num2=0;
    for (int i =1; i <= 7; ++i)
    {
        for (int j = 1; j <= 9; ++j)
        {
            cout << " ";
            while (num1 <=9)
            {
              cout << "   " << num1;
              num1++;
            }
        }

        while (num2 <=7)
        {
            cout << endl;
            cout <<num2  << " |   |   |   |   |   |   |   |   |   |   |" << endl;
            num2++;
           cout << "  ========================================";
        }
    }   cout << board[rows][cols];
}

is there any other way to do it as im not good in pointer.Im a very beginner.

you could declare the grid as a global variable, also I suggest you learn how to use pointers it's gonna be useful for scoping

Edited 4 Years Ago by zeroliken

I've edited it and it works.But Im not sure whether its correct or not

you could test it yourself by printing the array on the parent function (main) after the function call

.Is that now I can place the ship inside the grid? if yes how should I do it? please help me

should it be put randomly or does it have a pattern?

i've changed it and declare the grid as global function.It should place randomly as its players option to place their ships.

for the player side it shouldn't be hard just ask the user which position would he like to enter by asking the row and column or something similar to that and place that on the index of the array

for the random placement use the rand() function to get random inputs you could place on the index

for (int i =1; i <= 7; ++i)

Line 49: that is incorrect. All arrays start at 0, not 1, and go up to, but not including the number of elements in the array. So that line should be like this:
for(int i = 0; i < 7; ++i)

thank you. I've corrected it. now I dont know how to implement the ships inside this grid. any examples would help me? please.thank you .

This article has been dead for over six months. Start a new discussion instead.