Need Help With my Program

I need help writing this program, I am stuck, the following are the directions:

This is what I am supposed to do:

1.To start, create an array filled with the character O (for Ocean).
2.Generate a random number to be the index of the Battleship.
3.Allow the user three guesses to find the location of the Battle ship
4.After each guess you should print the board
5.Previously guessed locations should appear as the character X
6.A message should print if the user guesses a previously guessed location
7.After three (incorrect) guesses you win
8.If the user correctly guesses the location, they wine

Problems: It currently does not build it gives me a problem for Size in the statement: char board [SIZE]; & I am unsure why if in the rest of the program it doesn't do it. Also I am unsure on how to print the X's for what the user enters. Thanks.

This is what I currently Have:
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
#include <iostream>
#include <ctime> 
using namespace std; 

int main()
{
    srand (time(NULL));
    int SIZE= 10;
    char board [SIZE];
    int answer;
    fill(board, board+10, 'O');

    int index = rand() % 11;
    
    cout<<"Let's Play Battleship!!" << endl; 

    for (int i=0; i<SIZE; i++)
    {
        cout << board[i] <<" ";
    }
    
    cout<< endl;
    
    cout <<"Enter a guess from 0 to 10";
    cin >> answer;
    
    if (answer >= 0 && answer < SIZE)
        board[answer] = 'X';
    return 0;
}
You cannot initialize a static array with a non constant variable. try:

const int SIZE = 10;

or just

char board [10];

you also call a function called fill(char[], int, char), but have not defined the function anywhere in your code. you will want to do this, preferably above the main().

closed account (Dy7SLyTq)
fill is defined in i think iostream. its valid
the fill you are talking about fills the steam with a character. the OP wants to initialize the array with 'O'.

1
2
3
4
5
6
7
8
9
10
void fill(char[] board, int size, char Filler)
{

     if(size <= 0)
       return;

    for(unsigned int x = 0; x < size; x++
         board[x] = Filler;

};
My program is running now but now I am having trouble printing the X's for the number the user enters. Would I need a while loop before I ask the user to enter a number also, to check for the 3 times?
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

#include <iostream>
#include <ctime> 
using namespace std; 

int main()
{
    srand (time(NULL));
    const int SIZE= 10;
    char board [SIZE];
    int answer;
    fill(board, board+10, 'O');
    const int attemp = 3;
    int attempts = attemp;
    int index = rand() % 10;
    
    cout<<"Let's Play Battleship!!" << endl; 

    for (int i=0; i<SIZE; i++)
    {
        cout << board[i] <<" ";
    }
    
    cout<< endl;
    

    do
    {
        cout <<"Enter a guess from 0 to 10: ";
         cin >> answer;
        --attempts;
   
    }while ( --attempts && answer != index );
        
    if (answer >= 0 && answer < SIZE)
        board[answer] = 'X';
    return 0;
}
closed account (Dy7SLyTq)
the fill im talking about fills every element of the container with the character
http://www.cplusplus.com/reference/algorithm/fill/
and that makes no sense. of course he does. how else will it know to use 'O'
The program is running. I do not think the fill is currently working in this program as a function because then it wouldn't let me run it at all because I do not have it anywhere in my program and if you have a function it doesn't let you run it without having it (calling it) in your program. I think that works to print out the O's. I could be wrong but that is what it seems it is doing.
Thanks.
consider a "for" loop, and a function to print the board.


BTW single spaces are considered characters, and should be surrounded with ' '.
Topic archived. No new replies allowed.