0

I have this code that won't work correctly if there is no getchar()in the loop

#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) ;
}}

If i keep getchar() the output is normal:

enter image description here

But if i remove it then it stops after one turn :

enter image description here

please can someone explain why is this happening ?

Marwane
  • 333
  • 3
  • 13
  • [Don't use `gets`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). – mediocrevegetable1 Jun 05 '21 at 17:30
  • 1
    It is consuming the carriage return/line feed entered at the console, left after the input of `p.tel`. The loop iteration on the next pass uses `gets(p.nom)` (which is dreadful in itself, as that function is so evil it was removed from the standard library). If you read the documentation for `gets` you'll understand why having a crlf in the input stream after a previous formatted input will result in undesirable behavior from `gets`. And fyi, besides the horror of using gets, that `getchar` is also highly brittle. It can be circumvented by entering a space before hitting enter after `p.tel`. – WhozCraig Jun 05 '21 at 17:31
  • Mixing `scanf()` with `gets()` and then applying a kludge to deal with newlines. Don't mix your input methods - the same would apply with `fgets()`. [`fgets` doesn't work after scanf](https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf) – Weather Vane Jun 05 '21 at 17:33
  • @WhozCraig Thanks for explaining, but i still don't get it 100%, is it the '\n' that causing this ? – Marwane Jun 05 '21 at 17:37
  • The `scanf()` reads until the newline is found, which remains in the input buffer. In the next loop, that newline would be read by `gets()`, except the `getchar()` removes it - please read the duplicates. – Weather Vane Jun 05 '21 at 17:38
  • @AdrianMole thank you it did !! i didn't know about Buffer... – Marwane Jun 05 '21 at 17:39
  • This code uses some obsolete and dangerous C features that were removed from the language years ago. If you wrote this code, you probably need to find a better C textbook. If you've got this code somewhere, don't try to learn from it. – n. m. could be an AI Jun 05 '21 at 17:42
  • @n.1.8e9-where's-my-sharem. I did not know that thanks ! can you please give me just one alternative of scanf and gets , thanks again – Marwane Jun 05 '21 at 17:44
  • `gets` is not a part of C any more, use `fgets` instead. `main(void)` is not a part of C either, you need to use `int main(void)`. `scanf` is sort of OK if you are just starting off, but see [this](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html). – n. m. could be an AI Jun 05 '21 at 17:47
  • Please see [Implementing an alternative to scanf](https://stackoverflow.com/questions/51332418/implementing-an-alternative-to-scanf) – Weather Vane Jun 05 '21 at 17:47

0 Answers0