Reputation: 10179
So first of all I would like to paste my code in here for future references:
int dayNum = 0;
printf("\n\nEnter your date(1 - 30/31): ");
scanf("%d\n", &dayNum);
printf("\n\nEnter your note:");
char note[10000];
gets(note);
printf("%s", note);
The code is I think self-explanatory and easy-to-understand. However, here is a quick and short explanation from my side. This code just gets an integer input and stores it into a variable and then gets ready to take a string as an input and print it out to the console.
I expect the code to run like this:
Enter your date(1 - 30/31): <my_input>
Enter your note: <my_long_note>
<my_long_note> //prints my note that I wrote above
But, what is happening right now is like this(abnormal):
Enter your date(1 - 30/31): <my_input>
<my_long_note> //this is an input
Enter your note: <my_long_note> //this is an output
As you can see, it takes my note before printing out Enter your note:
.
Can someone tell me why is that happening? I am not quite sure of what did I do wrong in there.
Upvotes: 0
Views: 42
Reputation: 10179
Ah, so after another search, I found this:
Using scanf("%d%*c", &a)
. The %*c
term causes scanf to read in one character (the newline) but the asterisk causes the value to be discarded.
So my final code became this:
int dayNum = 0;
printf("\n\nEnter your date(1 - 30/31): ");
scanf("%d%*c", &dayNum);
printf("\n\nEnter your note:");
char note[10000];
gets(note);
printf("%s", note);
And, it worked.
Upvotes: 0
Reputation: 3289
You need to flush your output stream.. That means including a \n
in the printf, or by manually calling fflush(stdout)
.
Upvotes: 2