Arrays

Discussion in 'C++' started by krazykrisi, Jan 17, 2007.

  1. krazykrisi

    krazykrisi New Member

    Joined:
    Jan 17, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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 << " -";           }  }
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  3. krazykrisi

    krazykrisi New Member

    Joined:
    Jan 17, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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 << " -";
             }
    }
    
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice