Hi,
I'm just trying to figure out how to read into an array (word[5]) properly and there is something that I don't understand.
My program does simple thing - reads a word from user and then changes something in it.
But my concern is - let's say C = 3, I'm inputing words in this order:
cat
cool
tree
Everything works fine, but again: C = 3:
cat
horse //BOOM.
Process returned 0, program ended.
I know that the array holds only 4 letters and the last space is for the newline character, but I don't know why the program stops after inputing this one letter too many. Why it does not read the last 3rd word? Even in scanf I wrote that it should read only first 4 letters, but it didn't change this problem.
If there is something that I understand badly, please correct me.
Also, I know this is C++ forum but I believe there are people who know C language very well too.
Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
int main()
{
int C, i;
char word[5];
printf("Number of words: ");
do{
scanf( " %d", &C);
if(C<1 || C>50)
printf("Bad value!\n");
}while(C<1 || C>50);
for(i=0; i<C; ++i)
{
scanf("%4s", &word);
shorten(word);
}
return 0;
}
|