In addition to Rick York's solution, you have an issue with the initial
scanf()
Unfortunately,
scanf("%d", &t)
does not remove the trailing whitespace after a number (but it does remove
leading whitespace!). So when you call
gets()
in your
for
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
gets()
immediately following the scanf(), or perhaps a loop calling getchar(), which ends when it finds a '\n', you could also use
scanf("%d\n", &t);
, as long as you're confident that the input will always have a newline at the end.
And be careful with
gets()
. It does not check its input bounds, and will happily write data past the end of the string so if you have
char str[15];
gets(str);
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
fgets()
instead.