getchar() 在结构变量的成员中存储字符时不执行自身

我正在尝试使用 getchar 函数将字符存储在作为数组的结构变量成员中。问题是 getchar() 不会自行执行,尽管我的算法似乎是正确的。如果我尝试在程序的开头使用 getchar 它可以工作但是当我在程序的最后使用它时: if (input.pin==client[c].pin) getchar 不会执行自己。为什么?

typedef struct compte {


int pin;
char nom[10];
char prenom[10];
char ddn[11];
char adresse[40];
float solde;

}compte;




int main()
{


compte client[4]={
                  {3625,"Harris","Donald","26/08/1987","62 rue Calmette",35700.00 },{4512,"Anderson","Pamela","21/01/1966","5 rue Charles le Chauve",68400.66},{9912,"Barry","Sylvia","03/04/1971","8 rue Courtine",130755.65},{7164,"Cardec","Jonas","04/05/1941","51 rue de la vielle table",575441.41}

                 };
compte input;

int c=0;
int c2=0;
int L;
int i=0;




printf("BIENVENU A LA SOCIETE GENERALE");




printf("\nCode pin sur 4 chiffres: ");
scanf("%d",&input.pin);



 while ( input.pin != client[c].pin )
  {
    c++;

     if (c==4)
    {
        c=0;
        c2++;
        printf("\nCode pin introuvable.\nReessayez: ");
        scanf("%d",&input.pin);
    }

      while (c2==2 && input.pin != client[c].pin  )
      {
        c++;

        if(c==4)
        {
        printf("\n\nCode pin introuvable. Reprenez votre carte.\n");
        break;
        }
      }
  }




if (input.pin==client[c].pin)
 {

     printf("\n\nEntrez votre nom,prenom,date de naissance et adresse.\n\n");



     printf("NOM: ");
     scanf("%s",input.nom);

     
     printf("PRENOM: ");
     scanf("%s",input.prenom);

     
     printf("DATE DE NAISSANCE AU FORMAT 00/00/0000: ");
     scanf("%s",input.ddn);

     while( input.ddn[2] != '/'  &&    input.ddn[5] !=  '/' )
     {
         printf("\nVotre date de naissance doit être au format 00/00/0000.");
         printf("\nEntrez votre date de naissance au format 00/00/0000: ");
         scanf("%s",input.ddn);
     }

     
     printf("ADRESSE: ");
     while((L=getchar())!= '\n')
     {
         if (i<5)
         {
             input.adresse[i]=L;
             i++;
         }
     }
     input.adresse[i]='\0';


 }



    return 0;
}
qqq1100 回答:getchar() 在结构变量的成员中存储字符时不执行自身

printf("ADRESSE: "); 之前,我会再使用一个 getchar() 来消耗先前 scanf 之一留下的尾随空格。

,

尝试以下操作:

     printf("ADRESSE: ");
     short flag = 0;
     while(flag == 0) {
         L = getchar();
         if(L != '\n')
            flag = 1;
         if(flag == 1 && i < 5)
         {
            input.adresse[i]=L;
            i++;
         }
     }

它与使用 getchar() 在 while 比较中分配 L 有关。将标志声明与其余变量放在一起

本文链接:https://www.f2er.com/948.html

大家都在问