微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决循环 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 举报,一经查实,本站将立刻删除。