I am working out of a textbook learning c. One of the exercises is literally copying the code from the book to demonstrate using scanf. It gets user input for how many times something should be printed. Using a for loop to print out the string, the final characters of the string seem to have gotten jumbled up. Please see below and any help would be appreciated. So far I have tried removing the '\n' to see if maybe it is causing the issue. I have printed out the string before the loop to see if it is an error with the string but it seems to work fine outside the loop. Code below:
#include <stdio.h>
#include <string.h>
int main(void)
{
char message[10];
int count, i;
strcpy(message, "Hello, World!");
printf("Repeat how many times? ");
scanf("%d", &count);
printf("%s\n", message);
for(i = 0; i < count; ++i)
{
printf("%3d - %s\n", i, message);
}
return 0;
}
Results:
Repeat how many times? 3
Hello, World!
0 - Hello, Wor
1 - Hello, Wor
2 - Hello, Wor
Thanks in advance.