Thread: illegal vector index and checking bounds

  1. #1
    Unregistered
    Guest

    illegal vector index and checking bounds

    I need to check that on a game board (10X10 matrix) the user can't enter in values that are out of the matrix. Right now it only works if the placement of some number of the same character is horizontal. I had to set some specific values to check with my crappy debugger and it returns false (as it should) but when its vertical it gives me a illegal vector index error instead of asking for a new set of coordinates.

    this is the specific part with the problem--trimmed down some.
    Code:
    void PlaceChallengerShips  (apmatrix <char> &defenBoard)
    {
    	const int CARRIER = 5, BATTLESHIP = 4, SUBMARINE = 3, CRUISER = 3, DESTROYER = 2;
    	char alignment, rowLetter;
    	int column, numRow ;
    	bool valid = false;
    	//clrscr();
    	
    	cout<<"From the start of each ship enter the ROW letter,\nthe "
    		<<"COLUMN number, and a choice of <H>orizontal or <V>ertical Placement\n";
    	cout<<endl;
    	cout<<"Hit ENTER to continue\n";
    //	cin.get();	
    	do
    	{
    	  cout << "SHIP : Aircraft Carrier  "<<endl;
    	
    	  rowLetter='A'; column=11; alignment='V';
    	 
    	  numRow = int(rowLetter) - 65; //Adjust uppercase Letter to row number
    	  column = column - 1;			  //Adjust column to Matrix Index
    	  valid = ValidPlacement (defenBoard, numRow, column, CARRIER, alignment );
    			
    	 } while (!valid);
    
    and this is corresponding error producing part of ValidPlacement
    
    bool ValidPlacement ( const apmatrix <char> &grid, int numRow, int column, int ship, char alignment)
    {
    	bool valid = true;
    	
    //Row Check
    	if (numRow > 9)
    	{
    		valid = false;
    		cout<<"Illegal move. Enter a row letter from A-J\n";
    	}
    
    //Column Check
    	if (column > 9)
    	{
    		valid = false;
    		cout<<"Illegal move. Enter a column from 1-10\n";
    	}
    Anything obviously wrong there?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Force it:

    row = input % 10;
    row = row < 0 ? -row : row;

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    i dont think you've given enough information mate...

    where abouts is the case different for V and H??? you haven't really show it in there.

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Unregistered
    Guest
    thats thing, it really shouldn't be any different from V or H. If either is greater than 9, it shouldn't be allowed as imput. ie: returns false and continue through the do while loop. When I went through the debugger, it says its returning false for the imput values A 11 V or 0 10 V when it gets to ValidPlacement but I get that illegal vector index, and only when its vertical.

    If you'd like I can post the entirety of both functions.

  5. #5
    Unregistered
    Guest

    well I can't edit unregistered

    the only difference between V and H is how it prints out

    code here...this is the continuation of the first function
    Code:
    if ('V' == alignment)
    		for (int position = numRow; position < numRow + CARRIER; position++)
    			defenBoard [position] [column] = 'A';
    	  else
    		for (int position = column; position < column + CARRIER; position++)
    			defenBoard [numRow] [position] = 'A';

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    any chance of posting the entire project?? i'm really keen to see what you are getting at. zip up the project and post to the site.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  7. #7
    Unregistered
    Guest
    okay...I'm including the bit I was trying to debug before and a final copy of what I've done so far but the latter is kind of sloppy and has other errors as well. The project isn't anything special though, it's just battleship.

    http://www.angelfire.com/ak5/tempspace/battle.zip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array bounds checking
    By rohan_ak1 in forum C Programming
    Replies: 2
    Last Post: 08-26-2008, 10:16 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  5. Game matrix bounds checking
    By Panopticon in forum Game Programming
    Replies: 2
    Last Post: 02-04-2003, 10:30 PM