Sir, I wrote a simple program:
Code:
#include <stdio.h>
main()
{
char nam[100];
char place[50];
int a;
printf("\n\tHi, what's your name? ");
gets(nam);
printf("\n\tWelcome to our show, %s\n",nam);
printf("\n\tHow old are you? ");
scanf("%d",&a);
printf("\n\tHmm, you don't look a day over 22\n");
printf("\n\tTell me, %s, where do you live? ",nam);
gets(place);
printf("\n\tOh, I've heard %s is a lovely place\n",place);
}
which on execution should gives the following output:
Hi, what's is your name? David
Welcome to our show, David
How old are you? 27
Hmm, you don't look a day over 22
Tell me, David, where do you live? California
Oh, I've heard California is a lovely place
where underlined & italics are typed by the user.
I have not been able to find out anything wrong with my firts code, but on execution it runs fine as expected till 4th line of the output, but in the 5th line where it should ask for the input, prints the last two line as it is without the input (California here).
But when i repeat gets(place) one more time in my code as
Code:
printf("\n\tTell me, %s, where do you live? ",nam);
gets(place);
gets(place);
printf("\n\tOh, I've heard %s is a lovely place\n",place);
the program runs exactly as expected.
So sir, please anybody explain me the reason for why my first code did not run as expected and why the second code demands another gets(place) to run as expected.