0

It works if I read the string value first and then the int value. I tried use function gets instead of scanf because gets lets me read more than one word in the same line.

I also tried with fgets but it has the same problem.

I'm using cygwin 32 bit compiler version 2.874. I using codeblocks ide 13.12.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int i;
    char s[10];

    printf("Value int:\n");
    scanf("%d",&i);
    printf("%d\n",i);

    printf("Value string:");
    fflush(stdin);
    gets(s);
    printf("%s\n",s);

    getchar();
    return 0;
}
user3386109
  • 34,287
  • 7
  • 49
  • 68
rjcdz04
  • 23
  • 5
  • 2
    You should not use gets . Use instead fgets ==> fgets(s, 10, stdin); or scanf ==> scanf("%9s", s); – Michi Jun 21 '16 at 02:25
  • 1
    See http://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-buffer and http://stackoverflow.com/questions/3555108/multiple-word-string-input-through-scanf – NPE Jun 21 '16 at 02:25
  • `gets` is not standard (anymore) and every toolchain should yell at you using it one way or the other. **Never ever** use it! And `fflush(stdin)` invokes undefined behaviour. Also don't use it! – too honest for this site Jun 21 '16 at 02:28
  • `scanf` can read more than one word in one line too, if using correctly. And which IDE you are using is insignificant, while the compiler may concerns, and `cygwin` is not a compiler. – halfelf Jun 21 '16 at 02:30
  • See [Why the `gets()` function is to dangerous to be used](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) for a discussion of why you should not use it and what the alternatives are. You also need to check the return results. You should look at [Using `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin) for a discussion about why that is to be used with caution. – Jonathan Leffler Jun 21 '16 at 02:37

2 Answers2

2

After the call to scanf, a newline is left in the input buffer. So when you call gets, just that newline gets picked up.

Calling fflush(stdin) is undefined behavior as per the C standard, although MSVC supports it as an extension. You should instead use getchar to read the newline from the buffer.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Using 'getchar' after call 'scanf' and before call 'gets' was very useful. Thanks!. – rjcdz04 Jun 21 '16 at 22:21
  • I followed your advices so I used fgets. printf("Value int:\n"); scanf("%d",&i); printf("%d\n",i); getchar(); // This line was very useful. printf("Value string:"); fgets (s , sizeof s , stdin); printf("%s\n",s); Thanks again. – rjcdz04 Jun 21 '16 at 22:25
0

The second input will take the new line when you input the integer, try this with scanf:

scanf(" %s", s); // here the space will emit the previous newline or space

or better to use fgets which will limit the input to the size of the buffer:

fgets(s, sizeof s, stdin);

Also, do not use gets, it is dangerous and subject to buffer overflow. And do not call fflush on stdin, stdin is an input stream, flushing it is undefined behavior.

fluter
  • 13,238
  • 8
  • 62
  • 100
  • 1
    `gets` is not deprecated, but not even part of the C standard library. – too honest for this site Jun 21 '16 at 02:31
  • @Olaf It was part of C89 and C99, and removed in C11. – fluter Jun 21 '16 at 02:32
  • As I said: it is not part of the standard library. C89 is no ISO/IEC standard, that would be C90 and neither C90 nor C89 are C standard. That is only C11. See the forewords of the C99 and C11 documents (or the final drafts which are freely available). – too honest for this site Jun 21 '16 at 02:34
  • @Olaf not getting it, what is a standard library if not defined by the C standard? – fluter Jun 21 '16 at 02:35
  • @Olaf it is stated in forward section 6 of C11. – fluter Jun 21 '16 at 02:36
  • 1
    The point is, C90 and C99 are not standard, so they don't define what is part of the C **standard** library. – too honest for this site Jun 21 '16 at 02:38
  • Ah, got it. Just read paragraph 6 of the Foreword. I mean the very first clause. – too honest for this site Jun 21 '16 at 02:40
  • 1
    Olaf's point is that the C99 standard *officially* canceled and replaced the C90 standard, and the C11 standard officially canceled and replaced the C99 standard. As far as ISO is concerned, C11 (formally **ISO/IEC 9899:2011 (E)**) is the only C standard. More realistically, IMHO, it's perfectly reasonable to refer to the older standards as standards, as long as you're aware that they've been superseded. Plenty of compilers still support the older editions of the standard. – Keith Thompson Jun 21 '16 at 02:42
  • 1
    Ok, I see, maybe it is better to refer C11 as the *current* C standard, and C99 and others as the previous ones. :) – fluter Jun 21 '16 at 02:46