1

so, I am basically building an array of students with their evaluation.

I made a basic struct :

struct Eleve{
    char Nom[100];
    int Note;
};

struct Eleve *array;

Then asking for how many children I want to input into my array, then loop and asking information for each child. But, when I enter an input into scanf("%s\n", array[i].Nom); the code stop.

    int nbEleves;
    float total = 0;
    printf("Nombre?!\n");
    scanf("%d", &nbEleves);

    array = malloc(nbEleves * sizeof(struct Eleve));
    int i = 0;
    while (i < nbEleves) {
        printf("Nom? ");
        scanf("%s\n", array[i].Nom);
        printf("La note? ");
        scanf("%d\n", &array[i].Note);
        total = total + array[i].Note;
        i++;
    }

It doesn't even go on the next printf. I don't understand why, because I don't get any build error or execution errors on this line. Eventually I would have looked at the format if by any chance I'd get an error there, but nothing. No errors, just not getting to the next step of the program.

I think my scanf looks right, and I've got no warnings on it. So, what can prevent the code to go further?

The data I entered in Nom is test.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
provençal le breton
  • 1,428
  • 4
  • 26
  • 43
  • 1
    "*I think my scanf looks right*" -- `%s` in `scanf()` is never *right*, it's always a *buffer overflow*. It **needs a field width**. And `\n` is just another whitespace character, which are not distiguished by `scanf()`, so you could as well put a simple space instead. –  Jul 12 '17 at 08:07
  • @dasblinkenlight I did declared this way ` struct Eleve *array;` and entered 'test' as nom. You are right, I forgot to specify this. – provençal le breton Jul 12 '17 at 08:08
  • 1
    The problem *could* be that it's actually advancing to the next `printf()` and you just don't see the text because the output buffer isn't flushed. Then, the `scanf("%d")` is waiting for more input. –  Jul 12 '17 at 08:10
  • 3
    Interactive input with `scanf()` is in general a bad idea and has **many** pitfalls. Read for example [How to read / parse input in C? The FAQ](https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq). I also wrote a document on the subject: [A beginners' guide away from `scanf()`](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html). –  Jul 12 '17 at 08:11
  • 3
    @ArpitSvt [No, you don't cast the result of `malloc` in C](https://stackoverflow.com/a/605858/335858). (C++ is a different story). – Sergey Kalinichenko Jul 12 '17 at 08:23
  • Try adding a call to ` fflush(stdout)` after both `printf` calls in the loop, and see if this fixes your problem. – Sergey Kalinichenko Jul 12 '17 at 08:25
  • @FelixPalmen good read (not finished yet). Indeed, removing the \n solved the problem, but I will investigate on your statement about interactive input. Tahnks – provençal le breton Jul 12 '17 at 08:33

2 Answers2

2

You should not use scanf! This is an age old discussion that use of scanf requires care and is dangerous. Disadvantages of scanf.

Instead use fgets().

Check this answer How to read from stdin with fgets()? or this Not able to input a string with spaces in a loop in C

But if you still insist on using scanf(), this may be useful:

 while(i<nbEleves){
        printf("Nom? ");
        scanf(" %[^\n]",array[i].Nom);// remove the \n and notice the space before %[^\n]
        printf("La note? ");
        scanf("%d", &array[i].Note);
        total = total + array[i].Note;
        i++;
}

Also I suggest that you must free the memory you allocated using free() in order to prevent memory leaks.

free(array);
Pushan Gupta
  • 3,697
  • 5
  • 23
  • 39
  • I am not there yet in my courses, so maybe I am asking someting obvious but, which stream is the cmd? Because all I found is about files, even when the function is about streams. Is it STDIN? So basically I should get a `fgets (array[i].Nom, 100, STDIN)` ? – provençal le breton Jul 12 '17 at 09:22
  • @Zaphod yes you can say that console is stdin(Standard Input) in general. Its okay to ask obvious questions, have to learn this way or the other :) You can use fgets to get input from a file stream too. For standard console input you must use stdin. You want me on how to use fgets I can show you an example if you want? – Pushan Gupta Jul 12 '17 at 09:25
  • Tahnks. I indeed would like an example with fgets in my situation. I already had the code with scanf with the comments on my ask, but I would love to get a good way of doing it with fgets. – provençal le breton Jul 12 '17 at 09:27
  • @Zaphod Check this out: http://www.tutorialspoint.com/compile_c_online.php?PID=0Bw_CjBb95KQMMmtYNUM0dVFUWkE Just hit compile and then execute – Pushan Gupta Jul 12 '17 at 09:55
1

Remove the \n from scanf("%s\n", array[i].Nom); and scanf("%d\n", &array[i].Note); or press CTRL + D after you inserted the value.

gior91
  • 1,745
  • 1
  • 16
  • 19