Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8886771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:38:19+00:00 2026-06-14T21:38:19+00:00

For my class, I need to make a pretty basic Battleship game. However, I’m

  • 0

For my class, I need to make a pretty basic Battleship game. However, I’m new to programming and am running into some problems. First, let me explain my idea for the program. I’d like to keep two separate 2D arrays for each player’s gameboard. I have a function for placing each ship, and I will make other functions that incorporate those into one “turn” function. The gameboard will begin by being filled with O’s, and each ship placement will replace an O with the first letter of the ship. A hit will be designated as an X. Once the ships have been placed, a winner will be determined when a board only has X’s and O’s left. The main problem I’m running into right now is with my 2D arrays. I want to place them in a struct, but I’m running into a couple of errors that I don’t know how to handle. I’m getting an “expected unqualified-id before ‘.’ token” on the board.firstboard = line. The other error I’m getting is saying I’m missing a } but if I add one, it says it’s an extra. Is this related to the previous error? Thanks for any help! Also, I’m not sure if I provided my code correctly, so let me know if I didn’t and I’ll correct it!

    struct board
{
    static char firstBoard[10][10] ;
    static char secondBoard[10][10] ;
} ;

void initializeBoard()
{

    board.firstBoard =
    {
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
    } ;

    board.secondBoard =
    {
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
        {'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
    } ;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:38:21+00:00Added an answer on June 14, 2026 at 9:38 pm
    1. Making the arrays static members turns them into global variables. You don’t want that. If they are static, then all board objects will share the same two arrays.

    2. You can’t set arrays by assigning { ...data... } to them. You can only initialize arrays this way, and objects can only be initialized once. You can’t initialize globals from inside a function that way.

    Here is a suggested version:

    #include <cstring>
    
    struct board
    {
        static const int WIDTH = 10, HEIGHT = 10;
    
        board()
        {
            std::memset(firstBoard, 'O', sizeof(firstBoard));
            std::memset(secondBoard, 'O', sizeof(secondBoard));
        }
    
        char firstBoard[WIDTH][HEIGHT];
        char secondBoard[WIDTH][HEIGHT];
    };
    

    The changes are:

    1. The arrays are now fields of the board struct, rather than globals (static members).

    2. The initialization has been moved to a constructor for the board struct. You will not need to call it explicitly.

    3. Array initialization has been replaced with std::memset. This works well because the array element type is char. If they weren’t char, you might have to do something else to initialize them.

    4. Defined constants for array width and height.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a game and for the gun class, I need every
I am pretty new to python. I need to create a class that loads
So I'm still pretty new to Java and I'm trying to make a game.
I have a class that need to make some magic with every operator, like
I've spent 3 hrs starring at this and need some help. I'm pretty new
I have method in a class that I need to make sure is only
I'm porting some code from other language to Ruby, in my Class I need
I'm making a game right now and pretty much everything has its own class.
I'm pretty new to the world of pointers and have run into a problem
I'm in a basic programming class, and everything is done in pseudo code. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.