Hi there, I am wondering what I am doing wrong ss when a user inputs anything on the 10th row..the 'X' char is misplaced :s

Also, I cannot seem to limit the user from inputting BEYOND the board..I feel like I've taken a wrong step somewhere.

Thanks once again :)

Also, I do know that I could use structs and enums to make things easier for myself, but I have no idea on how they work.
I know the synatax but do not know how to implement them to my code.

#include <iostream>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <ctime>

using namespace std;
char matrix[10][10]; //Sets up grid for Ships
int m,n,x,y,a,i,j;
void welcomeScreen();
void board();

const int aircraftCarrier=5;
const int battleship=4;
const int destroyer=3;
const int submarine=3;
const int boat=2;

#define interLine "         ||      ---|---|---|---|---|---|---|---|---|---|\n"


void checkShip()
{
	int k,size=5;
	int direction;
	for(int m=0;m<5;m++){
	cout<<"Enter X value for your Aircraft Carrier"<<endl;
	cin>>x; --x;
	cout<<"Enter Y value for your Aircraft Carrier"<<endl;
	cin>>y; --y;
	cout<<"Enter Direction, 1[up], 2[down] : "<<endl;
	cin>>direction;
	cout<<endl<<endl;
		switch(direction){
		case 1:
			if((x-size+1)<0)
			{
				cout<<"Error!!!"<<endl;
			}
			else
			{
			for (k=0;k<5;k++)
				{
				matrix[x-k][y]='x';
				}
			}
			break;
			
		case 2:
			if((x-size+1)<0)
			{
				cout<<"Nuh uh!"<<endl;
			}
			else
			{
				for (k=0;k<5;k++)
				{
				matrix[x+k][y]='x';
				}
			}
			break;
		case 3:
			if((y-size+1)<0)
			{
				cout<<"Error!"<<endl;
			}
			else
			{
				for (k=0;k<5;k++)
				{
				matrix[x][y-k+1]='x';
				}
			}
			break;
		case 4:
			if((y-size-1)<0)
			{
				cout<<"Say WHAA?"<<endl;
			}
			else
			{
				for (k=0;k<5;k++)
				{
				matrix[x][y-k-1]='x';
				}
			}
			break;
		}
	}
}

int main()
{
	srand(time(NULL));
	welcomeScreen();
	
	for (int i = 0; i < 10; ++i) {
		for (int j = 0; j < 10; ++j) {
			matrix[i][j] = ' ';
		}
	}
	board();
	checkShip();
	system("CLS");
	board();
	cin.get();
	return 0;
}

void randomShip()
{
	srand(time(NULL));
	
}
void welcomeScreen()//Welcome Screen
{
	int choice;
	cout<<"\t Hi there, Welcome to Battleships. The game works as follows,"<<endl;
	cout<<"\t In this game each player places a fleet of several ships onto a"<<endl;
	cout<<"\t 2D grid in our case you will decide if you want to place your "<<endl;
	cout<<"\t ships manuallyor if you wish to set it randomly."<<endl;
	cout<<"\t The computer will always set their fleet randomly."<<endl;
	cout<<"\t Please enter your choice now:"<<endl;
	cout<<"\n\n\n\n";
	cout<<"\t 1. Set ships manually."<<endl;
	cout<<"\t 2. Set ships randomly."<<endl;
	cout<<"\t 3. Exit Game."<<endl;
	cout<<endl;
	cin>>choice;
		switch(choice)
		{
		case 1:
			void checkShip();
			break;
		case 2:
			void randomShip();
			break;
		case 3:
			cout<<"Thanks for playing, Goodbye :)"<<endl;
			exit(0);
			break;
		default:
			cout<<"You've chosen the wrong option, please try again \n";
			exit(0);
			break;
		}
	
} 

void board()//Sets up Board
{
	cout<<"                                  B                   \n"<<endl;
	cout<<"             --------------------------------------------"<<endl;
	cout<<"             --------------------------------------------\n"<<endl;
	cout<<"\n\t          1   2   3   4   5   6   7   8   9  10\n"<<endl;

	// 10 horizontal rows
	for(int i=0; i < 10;i++)
	{
		cout << "         ||     " <<(i + 1);
		// 10 vertical rows
		for(int j=0; j < 10;j++) {
			cout <<  " " << matrix[i][j] << "  ";
		}
		cout << endl << interLine;
	}
	cout << "\n\n\n";
}
This article has been dead for over six months. Start a new discussion instead.