Arrays

krazykrisi's Avatar, Join Date: Jan 2007
Newbie Member
I'm trying to make a program that uses an array to track a boat's position. Kind of like battleship but not quite that in depth. I want to have a grid of 5x5 and have spaces everywhere except where the boat is located and when the user enters a letter for which direction to move the boat it will print out the boat's new position. This is what I have so far:
Code:
#include <iostream>  using namespace std;  const int ROWS = 5;  const int COLUMNS = 5;  void Map( double[ROWS][COLUMNS] );	//declaration    int main()  {  	int Map[5][5];  	char direction;  	cout >> "Enter a letter to move Up, Down, Left, or Right or E to Exit: ";  	cin >> direction;  	Map[r][c];    	int x;  	cin >> x;  }  //--------------------------------------------------------------------------------------------------  // plot a 5 by 5 grid with B at position r, c  void plot(int r, int c)  {       for(int i=0; i<5; i++)         {         cout<< "\n " << i;         for(int j=0; j<5; j++)           if(i == r && j == c) cout << " B"; else cout << " -";           }  }
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Please consider reformatting your code for intelligibility. Spaghetti farms (an inch wide and a mile long) are fine for spaghetti, but suck at presenting your problem.
0
krazykrisi's Avatar, Join Date: Jan 2007
Newbie Member
sorry about that.
Code:
#include <iostream>
using namespace std;

int main()
{
	int x = 5;
	int y  = 5;
	int Map[10][10];
	char direction;

	if (x >= 10 && y >= 10)
	{
		cout >> "Out of range.";
	else if(x >= 10 || y >= 10)
		cout >> "Out of range.";
	else if(x <0 && y < 0)
		cout >> "Out of range.";
	else if(x < 0 || y < 0)
		cout >> "Out of range.";
	}

	cout >> "Enter a letter to move Up, Down, Left, or Right or E to Exit: ";
	cin << direction;
	switch (direction)
	{
	case 'U':
	case 'D':
	case 'L':
	case 'R':
	case 'E':
	}
	

	int x;
	cin >> x;
}
//--------------------------------------------------------------------------------------------------
// plot a 5 by 5 grid with B at position r, c
void plot(int r, int c)
{
     for(int i=0; i<5; i++)
       {
       cout<< "\n " << i;
       for(int j=0; j<5; j++)
         if(i == r && j == c) cout << " B"; else cout << " -";
         }
}
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
There are lots of errors like
After if the braces should be for the statements within the condition and should not also include the elses part.
Code:
if (x >= 10 && y >= 10)
{
	cout >> "Out of range.";
}
else if(x >= 10 || y >= 10)
{
	cout >> "Out of range.";
else if(x <0 && y < 0)
{
	cout >> "Out of range.";
}
else if(x < 0 || y < 0)
{
	cout >> "Out of range.";
}
Also cout and cin operators are interchanged, operator is << and cin >> and you should be using that one.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Your errors were rife, and also included a redefinition of x at main's local scope. I would suggest that you might want something like this.
Code:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void plot (int r, int c);

int main()
{
	int x = 5;
	int y  = 5;
	bool rangeError;
	bool exitFlag = false;
	char direction;

	plot (x, y);
	while (true)
	{
		cout << "Move U, D, L, or R; E to exit: ";
		cin >> direction;
		direction = toupper (direction);
		rangeError = false;
		switch (direction)
		{
		case 'U':
			if (y == 0) rangeError = true; else y--;
			break;
		case 'D':
			if (y >= 9) rangeError = true; else y++;
			break;
		case 'L':
			if (x == 0) rangeError = true; else x--;
			break;
		case 'R':
			if (x >= 9) rangeError = true; else x++;
			break;
		case 'E':
			exitFlag = true;
			break;
		default:
			cout << "Invalid input. ";
			continue;
		}
		if (exitFlag) break;
		if (rangeError) cout << "You're up against the wall, Bubba. Try again: ";
		else 
			plot (x, y);
		cin.sync ();
		cin.clear ();
	}
}
//--------------------------------------------------------------------------------------------------
// plot a 10 by 10 grid with B at position r, c
void plot (int r, int c)
{
	for (int x = 0; x < 50; x++)
	{
		cout << "\n";
	}
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
			if (i == c && j == r) cout << " B"; else cout << " - ";
		cout << "\n\n";
	}
	cout << endl;
}
Sample output:



 -  -  -  -  -  -  -  -  -  -

 -  -  -  -  -  -  -  -  -  -

 -  -  -  -  -  -  -  -  -  -

 -  -  -  -  -  -  -  -  -  -

 -  -  -  -  -  -  -  -  -  -

 -  -  -  -  -  B -  -  -  -

 -  -  -  -  -  -  -  -  -  -

 -  -  -  -  -  -  -  -  -  -

 -  -  -  -  -  -  -  -  -  -

 -  -  -  -  -  -  -  -  -  -


Move U, D, L, or R; E to exit:
The output is rather ugly but is one way to portably "clear the screen" and rewrite the new contents of the map. To do otherwise, you will need to throw portability out the window and use code specific to a particular implementation, such as Windows or a Unix derivative on an Intel platform, or whatever else you might be dealing with.