串的模式匹配,运行不了,错哪儿了?该如何解决

串的模式匹配,运行不了,错哪儿了???
#include <iostream>
#define MaxSize 100

using namespace std;

typedef struct sqstring
{
char *ch;
int length;
}SqString;

void InitStr(SqString &str)
{
str.ch = new char[MaxSize];
str.length = 0;
}

void StrAssign(SqString &str, char a[])
{
int i;
for(i = 0; a[i] != '\0'; i++)
{
str.ch[i] = a[i];
}
str.length = i;
}

int Index(SqString t, SqString p, int pos)
{
int i, j;
i = pos; j = 1;
while(i <= t.length && j <= p.length)
{
if(t.ch[i] == p.ch[j])
{
++i; ++j;
}
else
{
i = j - i + 2;
j = 1;
}
}
if(j > p.length) return i - p.length;
else return 0;
}

void get_next(SqString p, int next[])
{
int i, j;
i = 1; next[1] = 0; j = 0;
while(i < p.length)
{
if(j == 0 || p.ch[i] == p.ch[j])
{
++i; ++j; next[i] = j;
}
else j = next[j];
}
}

int Index_KMP(SqString t, SqString p, int pos, int next[])
{
int i, j;
i = pos; j = 1;
while(i <= t.length && j <= p.length)
{
if(j == 0 || t.ch[i] == p.ch[j])
{
++i; ++j;
}
else
{
j = next[j];
}
}
if(j > p.length) return i - p.length;
else return 0;
}

int main()
{
SqString p, t;
InitStr(p);
InitStr(t);
int pos;
cout<<"please input pos: ";
cin>>pos;
char a1[] = "abcac";
StrAssign(p, a1);
int next[5];
get_next(p, next);
char a2[] = "ababcabcacbab";
StrAssign(t, a2);
if(Index(p, t, pos))
{
cout<<"模式不匹配!"<<endl;
}
else
{
cout<<"B-F: "<<Index(p, t, pos)<<endl;
}
if(Index_KMP(p, t, pos, next))
{
cout<<"模式不匹配!"<<endl;
}
else
{
cout<<"KMP: "<<Index_KMP(p, t, pos, next)<<endl;
}
return 0;
}

------解决方案--------------------

int Index(SqString t, SqString p, int pos)
{
int i, j;
i = pos; j = 1;
while(i <= t.length && j <= p.length)
{
if(t.ch[i] == p.ch[j])
{
++i; ++j;

}
else
{
i = j - i + 2;
j = 1;
}
}
if(j > p.length) return i - p.length;
else return 0;
}
while(i <= t.length && j <= p.length)
这句话永远为真 死循环 i最后在5与-2间 来回赋值 
检查算法是否正确 
while内处理出错