Your first and most obvious problem is with the left over newline. When you use scanf()
here:
printf("\nDo you want write another text? (1=yes) -> ");
scanf("%d", &j);
}
and you use the %d
format specificer, the function is looking for a number, when you enter a number really you're entering a number and a newline character
> 1<enter key> // which means on stdin you're getting '1''\n'
scanf()
only picks up the 1 and leaves the newline which your gets()
function then picks up, so it looks like it's skipping the input. All you need to do is consume that newline character, one quick fix would be to consume it with getchar()
:
printf("\nDo you want write another text? (1=yes) -> ");
scanf("%d", &j);
getchar();
}
Now your program works as you'd expect.
Other issues of note:
- Your main should really be returning an
int
type, even if it's just areturn 0
- You shouldn't be using
gets()
, even then man page forgets()
says Never use gets(). That's usually a good indication not to. ;) So replace that line withfgets(testo, sizeof(testo), stdin);
- You missed a performance specificer here:
printf("\nThe text is composed by % characters\n", cch);
so you're getting garbage output, that should have been%d