循环 C 中 getchar() 的目的是什么?

问题描述

如果循环中没有 getchar(),我的代码将无法正常工作

#include <stdio.h>
#include <string.h>
main(void) {
typedef struct personne{
    char nom [20] ;
    char prenom [15] ;
    int age ;
    char tel[12] ;
} personne ;
personne p;

FILE * f ;
if ((f = fopen("Contacts.dat","ab")) == NULL) {
    printf("\nImpossible de lire le fichier commandes.dat\n");
}
else{
    printf (" Saisie des contacts a stocker dans le fichier\n");
    printf (" --- pour finir la saisie,donnez un nom ‘vide' ---\n");
    while ( printf("nom : "),gets(p.nom),strlen(p.nom)!=0 ){
        printf ("prenom : ") ;
        gets(p.prenom) ;
        printf ("age : ") ;
        scanf("%d",&p.age) ;
        printf("telephone : ") ;
        //gets(p.tel) ;
        scanf("%s",&p.tel) ;
        fwrite(&p,sizeof(personne),1,f);
          getchar(); //This is the problem
    }

    fclose (f) ;
}}

如果我保持 getchar() 输出是正常的:

enter image description here

但是如果我将其移除,它会在转一圈后停止:

enter image description here

有人能解释一下为什么会这样吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)