Some code I'm working on

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply
metkillerjoe




Wordewatician 500

Posts: 1267
Joined: Thu Aug 03, 2006 3:35 pm
Contact:

Some code I'm working on

Post by metkillerjoe »

Both Player and shipPlaceDir are strings.


Coded in C++.

Code: Select all

void playerInput()
{	
	shipType = Player[0];
   	shipColumn[0] = Player[3];
   	shipColumn[1] = Player[4];
   	int Playerlen = Player.length() - 1; 
   	int dirBegin = Player.find(' ') + 2;
   	
  	cout << Playerlen << " " << dirBegin; 	
   	//Converts the alphanumerical character to ASCII integer
   	e = static_cast<int>(Player[2]) - 65;
   	//column indication
	d = atoi(shipColumn) - 1;
	if(d == 104)
		d = 9;
	//Puts "down" "up" or any other direction from the Player to shipPlaceDir
	for(x = 0; x <= (Playerlen - dirBegin); x++)
	{
		int y = dirBegin + x;
		shipPlaceDir[x] = Player[y];
	}
		cout << shipPlaceDir; 			
}

Battleship playerInput() function copyright 2008 Mark Galesic.
(not that u would want to copy my crappy broken code ;) ).

This code is part of a Battleship game I am programming. This code is for placement of ships, and the way the syntax for input goes, one must provide a start coordinate and then the direction of the end coord. For example, 5(A1 right) would mean you want a carrier (5) put from A1 - A6. The for statement is part of the program that puts the directional statement ("down", "right", etc) into a separate string (shipPlaceDir) for use in other code.

The thing that is bugging me is the "for" statement. For some reason, no errors are given when compiling, but my friend told (we were in the computer lab he had to leave but said he'd help if I didn't get through with the problem) me that its a seg (I'm guessing short for "segment") error.

Anyway, what he thought was that the loop was doing something out of bounds out of one of the strings. Well, I rechecked my math, and saw what I need to do, but I still get the seg error.

Can anybody give me a hint as to what the problem is?
Image
Post Reply