First piece of advice, don't use gets()
- there is no way to use it safely so as to avoid potential buffer overflow (it's one of the few things that have even been removed from the ISO standard, as of C11). Instead, use fgets()
, which can limit what's read in.
As to your specific problem, scanf(" %d", &numberOutcomes)
will, after skipping whitespace, consume enough characters in the input stream to populate numberOutcomes
, and no more. Importantly, it will not consume trailing whitespace such as the \n
character that was put there when you pressed ENTER to submit your input.
Hence, on the gets()
call, it will simply get a blank line.
Both of those problems can be solved in you choose either the scanf()
-style input or line-based input. For the former case, scanning the next item will (usually) skip the whitespace in the input stream, for the latter, reading lines reads the whole line including trailing whitespace.
You can make it work by mixing the two but it's a bit more effort.
My advice would be to use line-based input for everything and then sscanf
specific data out of the string you read. This has the advantages of:
- avoiding the exact problem you've found; and
- allowing you to
sscanf
multiple times on the same line if you need to handle multiple line formats.
For that, a rock-solid line-input routine (like this one) is very handy.