Dc Motor with encoder code issue with Global variable reverting to zero

I am trying to write the code to run my DC motor.

When the user enters a value(the target value) into the serial monotor the code is supposed to cause the encoder to increment the value of Lpos until it equals Cpos(the target value). when Cpos = Lpos then motor should stop.
The problem I'm having is that Lpos keeps losing it's value and reverts to zero.
Thanks in advance for any help.

See code below:

// connect motor controller pins to Arduino digital pins
// motor one
int enA = 9;
int in1 = 8;
int in2 = 7;
int lastPos = 0;
int curPos = 0;
int Mspeed = 0;
int intDirection;
//int getDirection;
int integerValue = 0;
char incomingByte;
int Lpos;
int Cpos;
void getDirection(int& Lpos);

//The sample code for driving one way motor encoder
const byte encoder0pinA = 2;//A pin -> the interrupt pin 0
const byte encoder0pinB = 4;//B pin -> the digital pin 4
byte encoder0PinALast;
int duration;//the number of the pulses
boolean Direction;//the rotation direction
void setup(){
Serial.begin(9600);
EncoderInit();//Initialize the module
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
}

void EncoderInit()
{
Direction = true;//default -> Forward
pinMode(encoder0pinB,INPUT);
attachInterrupt(0, getDirection, CHANGE);
}

void RunMotor()
{
Serial.println ("RunMotor");
Serial.println (intDirection);
analogWrite(enA, 200);
}

void gotoPos()
{
// this function will run the motors across the range of possible speeds
// note that maximum speed is determined by the motor itself and the operating voltage
// the PWM values sent by analogWrite() are fractions of the maximum speed possible
// by your hardware
// turn on motors
//determine direction of rotation
if (Cpos > Lpos) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
intDirection = 1;
RunMotor();
}
else
if (Cpos < Lpos) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
intDirection = -1;
RunMotor();
}
}

void getDirection(int& Lpos)
{
int Lstate = digitalRead(encoder0pinA);
if((encoder0PinALast == LOW) && Lstate==HIGH)
{
int val = digitalRead(encoder0pinB);
if(val == LOW && Direction)
{
Direction = false; //Reverse
//intDirection = 1;
Lpos = --Lpos;
Serial.print("-Lpos = ");
Serial.println (Lpos);
}
else if(val == HIGH && !Direction)
{
Direction = true; //Forward
//intDirection = -1;
Lpos = ++Lpos;
Serial.print("+Lpos = ");
Serial.println (Lpos);
}
}
encoder0PinALast = Lstate;

if (Lpos == Cpos)
{
// now turn off motors
Serial.println ("motor is off");
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
}

void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
integerValue = 0;
while(1) {
incomingByte = Serial.read();
if (incomingByte == '\n') break;
if (incomingByte == -1) continue;
integerValue *= 10;
integerValue = ((incomingByte - 48) + integerValue);
}
Cpos= integerValue;
Serial.print ("Initial Cpos = ");
Serial.println (Cpos);
Serial.print ("Initial Lpos = ");
Serial.println (Lpos);
gotoPos();
}
}

Don't do serial I/O in interrupt context.

Got it. Thanks!
I got it working but the accuracy was lacking due to the problem you pointed out. Stepper motor works so much better with less code.