Apr 2, 2013 at 7:14pm UTC
aaa
Last edited on Apr 4, 2013 at 12:22am UTC
Apr 2, 2013 at 7:36pm UTC
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';
Apr 2, 2013 at 7:40pm UTC
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
Apr 2, 2013 at 7:44pm UTC
and what would I declare boardSize too?
Apr 2, 2013 at 7:48pm UTC
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.:)
Apr 2, 2013 at 7:54pm UTC
@Vlad from Moscow can you please help me with my program I really need help
Apr 2, 2013 at 7:55pm UTC
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 Apr 2, 2013 at 8:04pm UTC
Apr 2, 2013 at 8:04pm UTC
You have to declare boardSize. I used it because hardcoding in an array size is no good.
const int boardSize = 10;
char board[boardSize];
Apr 2, 2013 at 8:20pm UTC
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 Apr 2, 2013 at 8:20pm UTC
Apr 2, 2013 at 8:23pm UTC
Also, make SIZE const and add it to the std::fill method.
Apr 2, 2013 at 8:31pm UTC
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
Apr 2, 2013 at 8:39pm UTC
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 Apr 2, 2013 at 8:41pm UTC
Apr 2, 2013 at 8:57pm UTC
How would I print out the battleship?? that is basically the only thing I need thanks!1