0
void readStateTransitions(state *s){

    char kok[20];
    char y;

    int j=0;

    do{
        char alpha=' ';
        printf("Enter the alphabet\t ");
        scanf("%c", &alpha );

        //s->TrasitionsArray[j]->label=alpha;

    s->TrasitionsArray[j] = (struct trans*)malloc(sizeof(struct trans));   
    s->TrasitionsArray[j]->from = (struct state *)malloc(sizeof(struct state));
    s->TrasitionsArray[j]->to = (struct state *)malloc(sizeof(struct state));
    printf("For %s",s->label);
    //printf("Enter the next state for %s under %c",StatesArray[i].label,alphabet[j]);

    printf("Enter the next state under %c",alpha);
    scanf("%s",kok);
    for(int k=0;k<StatesNumber;k++){
        if(strcmp(StatesArray[k].label,kok)==0){
            s->TrasitionsArray[j]->to=&StatesArray[k];
        }
    }
    s->TrasitionsArray[j]->from=s;
    s->TrasitionsArray[j]->label=alpha;
    j++;
        printf("\n\n more transitions..??(y/n)");
    }while(getch()=='y');

}

我有这段代码,这里要求用户输入两个变量,一个用于 alpha(char),第二个用于 kok(string),但问题是程序忽略了变量 alpha 并直接移动到变量 kok

图片链接http://imageshack.us/photo/my-images/194/capturejey.jpg/

4

1 回答 1

1

你需要消耗最后的\n。由于您只想读取 alpha 变量的单个字符,请尝试使用以下内容:

alpha = getchar();
while (getchar() != '\n');
//scanf("%c", &alpha); //Instead of this

刚刚在您的代码中对其进行了测试,对我有用。

于 2012-07-03T18:18:33.960 回答