Help with program involving arrays?

closed account (EApGNwbp)
aaa
Last edited on
Try

1
2
3
char board[10]; // declare array

std::fill(board, board+10, 'O'); // fills the board with the char O 
closed account (EApGNwbp)
@IceThatJew

This is what I have so far I don't know where to go from there how do I display the next line of code where they are all Zeros a for loop?


#include <iostream>
using namespace std;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std; 

int main()
{
    char board [10];
    fill(board, board+10, 'O');

    int random= rand() % 11;
    
    cout<<"Let's Play Battleship!!";
    for (int i=0; i<board; i++)
    {
        
    }

    
    return 0;
}
inside your for loop simply cout the board array.

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

After you take in their guess just replace the 'O' with an 'X'.

if (answer >= 0 && answer < boardSize)
board[answer] = 'X';
closed account (EApGNwbp)
wow thanks! but hey it says it has an error because when i place i with board it fails and says 'comparison between pointer and integer(int and char)
How can i fix this? thanks for all your help
closed account (EApGNwbp)
and what would I declare boardSize too?
By the way the sample output is incorrect!:)

Let's play Battleship!

O O O O O O O O O O

Enter a guess from 0 to 10: 3

You missed by Battleship!

O O O X O O O O O O

There are 11 numbers from 0 to 10 but the output shows only 10 positions.:)
closed account (EApGNwbp)
@Vlad from Moscow can you please help me with my program I really need help
closed account (EApGNwbp)
this is what I have but it doesn't build...
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


#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;
}
Last edited on
You have to declare boardSize. I used it because hardcoding in an array size is no good.

const int boardSize = 10;

char board[boardSize];
closed account (EApGNwbp)
Okay I did thanks now this is what I have should I use a for loop to print out next?? I need help with this thanks#include <iostream>
#include <ctime>
using namespace std;
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
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 should introduce one more loop that will iterate three times that corresponds to the number of attempts of a user.

1
2
3
4
5
6
7
8
9
10
const int TOTAL_ATTEMPTS = 3;
//...

int attempts = TOTAL_ATTEMPTS;

while ( attempts )
{
//   some other stuff
   --attempts;
}
As I said above a user will never win if the random number will be equal to 10 and the random number can be equal to 10 if you are using expression

int index = rand() % 11;
Last edited on
Also, make SIZE const and add it to the std::fill method.
Maybe the loop should look as

1
2
3
4
do
{
   // some other stuff
} while ( --attempts && answer != index );  
closed account (EApGNwbp)
WOW thanks so much!!! and in the do while loop I would include the arrays to output the battleship? I am not sure how I would do that exactly
1
2
3
4
5
6
7
8
9
cout<<"Let's Play Battleship!!" << endl;

int attempts = TOTAL_ATTEMPTS;


do
{
   // some other stuff
} while ( --attempts && answer != index );   






closed account (EApGNwbp)
This is what I have so far: and where would I use index in the program?
Thanks for all your help guys I really appreciate it

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

#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 TOTAL_ATTEMPTS = 3;
    int attempts = TOTAL_ATTEMPTS;
    int index = rand() % 10;
    

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

    cout<< endl;
    
    cout<<"Let's Play Battleship!!" << 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;
}
Last edited on
closed account (EApGNwbp)
How would I print out the battleship?? that is basically the only thing I need thanks!1
1
2
3
4
5
6
7
8
9
10
11
12
    do
    {
        for (int i=0; i<SIZE; i++)
        {
            cout << board[i] <<" ";
        }
        cout << endl;

        cout <<"Enter a guess from 0 to 10: ";
         cin >> answer;
        // other stuff...
    }while ( --attempts && answer != index );
Topic archived. No new replies allowed.