In addition to Rick York's solution, you have an issue with the initial
does not remove the trailing whitespace after a number (but it does remove leading
whitespace!). So when you call
loop the first time, you get an empty string - the contents of the input buffer after the number read in, which in this case is probably
the newline character.
There's various ways around this. You could call
immediately following the scanf(), or perhaps a loop calling getchar(), which ends when it finds a '\n', you could also use
, as long as you're confident that the input will always have a newline at the end.
And be careful with
. It does not check its input bounds, and will happily write data past the end of the string so if you have
and someone enters "The quick brown fox jumps over the lazy dog", then you get the dreaded Undefined behavior - Wikipedia
] Its much safer to use