Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在while循环中退出scanf_C_Scanf - Fatal编程技术网

在while循环中退出scanf

在while循环中退出scanf,c,scanf,C,Scanf,我有这张代码: listCh remplir(){ char rep; listCh l,aux,p; printf("1-veuillez entrer un element?(n pour quitter)\n"); scanf("%c",&rep); if(rep=='n') l=NULL; else{ l=malloc(sizeof(listCh)); printf("2-Donnez

我有这张代码:

listCh remplir(){
    char rep;
    listCh l,aux,p;
    printf("1-veuillez entrer un element?(n pour quitter)\n");
    scanf("%c",&rep);
    if(rep=='n')
        l=NULL;
    else{
        l=malloc(sizeof(listCh));
        printf("2-Donnez la valeur d element!\n");
        scanf("%d",&l->valeur);
        p=l;
    }
    while(rep!='n'){
        printf("voulez-vous ajouter un nouveau element de la list? (n pour quitter)\n");
        scanf("%c",&rep);
        if(rep!='n'){
            aux=malloc(sizeof(listCh));
            printf("Donnez la valeur d element!\n");
            scanf("%d",aux->valeur);
            p->suiv=aux;
            p=aux;
        }
        else{
            p->suiv=NULL;
        }

    }
    return l;
}
执行时没有错误! 但是,问题是我的程序逃避了while循环中的第一个scanf函数

我没有找到解释

我需要一些帮助


非常感谢:)

它可能是从您的第一次
scanf


尝试将
getchar()

尝试将
getchar()
放在循环的scanf前面

scanf()
是字符变量时,请务必记住在
%c
前面放一个空格。有点不寻常,但这就是如何在获取真实字符之前跳过输入流中的所有缓冲空间


scanf(“%c”和&rep)应该是
scanf(“%c”和&rep)

scanf()
字符变量时,请始终记住在
%c
前面加空格。有点不寻常,但这就是如何在获取真实字符之前跳过输入流中的所有缓冲空间


scanf(“%c”和&rep)应该是
scanf(“%c”和&rep)

是否可能重复真的没有错误<代码>列表l;l=malloc(sizeof(listCh))似乎很奇怪。1)scanf(“%d”&(aux->valeur))(添加和)检查编译器设置。一个好的编译器会对此发出警告。2)
scanf(“%c”和&rep)-->
scanf(“%c”和&rep)(添加空格)是否确实没有错误的可能重复<代码>列表l;l=malloc(sizeof(listCh))似乎很奇怪。1)scanf(“%d”&(aux->valeur))(添加和)检查编译器设置。一个好的编译器会对此发出警告。2) 
scanf(“%c”和&rep)-->
scanf(“%c”和&rep)(添加空格)