1

I'm trying to store characters in a struct variable's member which is an array, using getchar function. The problem is getchar() doesn't execute itself, although my algorithm seems to be right. If I try to use getchar at the beginning of the programm it works but when I'm using it inside the: if (input.pin==client[c].pin) at the very end of the programm, getchar doesn't execute itself. why?

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;
}
MrFeli45
  • 17
  • 4
  • Does this answer your question? [Why doesn't getchar() wait for me to press enter after scanf()?](https://stackoverflow.com/questions/1391548/why-doesnt-getchar-wait-for-me-to-press-enter-after-scanf) – Isaiah Aug 01 '21 at 18:54
  • I thank you for your respond. Can you write the right code to me so I can test it to see if it works? – MrFeli45 Aug 01 '21 at 19:02

2 Answers2

1

Before printf("ADRESSE: ");, I would use one more getchar() to consume trailing whitespace left from one of prior scanfs.

0

Try the following:

     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++;
         }
     }

It has to do with using getchar() to assign L in the while comparison. Put the flag declaration with the rest of your variables

  • 1
    Thanks for your help , it is working! The thing is I don't understand what "short flag =0;" neither "flag=1". Can you explain what it means in detail please? thanks. – MrFeli45 Aug 01 '21 at 19:51
  • It is a var of type short, which is a signed integer that occupies less memory than an int var. – Ignacio de Miguel Aug 02 '21 at 17:41