I think you will find it mush easier to use a two dimensional array for your grid rather than computing the index yourself every time. Let the compiler do the work.
Your logic allows a ship to wrap from one row to the next.
1 2
|
const int SIZE = 10;
char grid[SIZE][SIZE];
| |
It's a good idea to use a named constant for the size of your grid. This allows you to change the size of your grid easily without having to change every "10" in your program.
This doesn't do what you think it does:
1 2 3
|
if (n<1||n>10)
if (m<1||m>10)
cout << "Invalid coordinates!" << endl;
| |
Both if statements have to be true to display "Invalid coordinates". You want an OR condition.
1 2
|
if (n<1||n>SIZE||m<1||m>SIZE)
cout << "Invalid coordinates!" << endl;
| |
Your logic for determining if a ship is sunk flawed. It only works if the last hit is at the front of the ship. You're also equating points with ships. They're not the same. If you have 3 hits, you're subtracting 3 from the number of ships.
The following code will go out of bounds when i is 98.
|
if (grid[i]=='H' && grid[i+1]=='H' && grid[i+2]=='H')
| |
You will also find it a good idea to break your game up into smaller functions.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.