Issue with string in function

I made a function:

void drive(string direction, char speed)
{
	if (direction == "forward")
	{
		motor[backLeftMotor] = speed;
		motor[backRightMotor] = speed;
		motor[frontLeftMotor] = speed;
		motor[frontRightMotor] = speed;
	}

	if (direction == "backwards")
	{
		motor[backLeftMotor] = -speed;
		motor[backRightMotor] = -speed;
		motor[frontLeftMotor] = -speed;
		motor[frontRightMotor] = -speed;
	}

	if(direction == "right")
	{
		motor[backLeftMotor] = speed;
		motor[backRightMotor] = -speed;
		motor[frontLeftMotor] = speed;
		motor[frontRightMotor] = -speed;
	}

	if(direction == "left")
	{
		motor[backLeftMotor] = -speed;
		motor[backRightMotor] = speed;
		motor[frontLeftMotor] = -speed;
		motor[frontRightMotor] = speed;
	}

	if(direction == "leftBack")
	{
		motor[backLeftMotor] = -speed;
		motor[backRightMotor] = 0;
		motor[frontLeftMotor] = -speed;
		motor[frontRightMotor] = 0;
	}
	if(direction == "rightBack")
	{
		motor[backLeftMotor] = 0;
		motor[backRightMotor] = -speed;
		motor[frontLeftMotor] = 0;
		motor[frontRightMotor] = -speed;
	}
	if(direction == "leftForward")
	{
		motor[backLeftMotor] = speed;
		motor[backRightMotor] = 0;
		motor[frontLeftMotor] = speed;
		motor[frontRightMotor] = 0;

		if(direction == "rightFoward")
		{
			motor[backLeftMotor] = 0;
			motor[backRightMotor] = speed;
			motor[frontLeftMotor] = 0;
			motor[frontRightMotor] = speed;
		}

		if(direction == "stop")
		{
			motor[backLeftMotor] = 0;
			motor[backRightMotor] = 0;
			motor[frontLeftMotor] = 0;
			motor[frontRightMotor] = 0;
		}
	}
}

I call the function here:

			if(SensorValue[LeftEncoder] < 4927 || SensorValue[RightEncoder] < 4927)
			{
				drive(forward, 127);
			}

			else
			{
				drive(stop,0);
				outtake(full);
				wait10Msec(100);
				outtake(stop);
				wait10Msec(15000);
			}

The code gives me the following errors:

Error:Undefined variable ‘forward’. ‘short’ assumed.

Error:Expression does not fit parameter. Call to ‘drive’. Parameter: ‘unsigned string & direction’ is ‘forward’ of type ‘short’.

Error:‘const’ expressions does not fit. Call to ‘drive’. Parameter: ‘unsigned string & direction’ is ‘“stop”’ of type ‘string’.

Error:‘const’ expressions does not fit. Call to ‘outtake’. Parameter: ‘unsigned string & speed’ is ‘“full”’ of type ‘string’.

Error:‘const’ expressions does not fit. Call to ‘outtake’. Parameter: ‘unsigned string & speed’ is ‘“stop”’ of type ‘string’.

I have tried to encapsulate my string inside of “quotes” and it didn’t help (like this


drive("forward",20);

I’m honestly not seeing the issue that the compiler is finding. I’ve declared both variables inside my function and when i called it.

The reason you are seeing this issue is because strings are essentially an array of characters; without going into too much detail, there are a couple of ways that you can solve this.

The first way (and probably the easiest) is to simply change the parameter type from a string to a character pointer:

Another solution is to keep the parameter definition as a string, but to declare a separate string variable in task main (or wherever you are calling the function from), storing the desired value in the variable, and then passing the value of the variable to the function:

There are also many other ways that you can solve this (passing and using a different data type, such as an integer; using #define macros; using an enumeration, etc) but these two are the easiest. Please let us know if you have any other questions.

Ok, thanks for the reply and the help.