Hey, I have a problem taking input from the terminal. I need to read an integer first, then a string. But when I press enter after entering the integer, the string doesn't take any input. Yes, I did try using a fflush(stdin) between the 2 inputs, still doesn't work. I am aware of the difference between gets and fgets and tried both, still doesn't work.
This is the code I am using. Please have a look.
#include<stdio.h>
#include<malloc.h>
int id = 0;
char* name;
int main(void)
{
name = (char*)malloc(200 * sizeof(char));
printf("\nEnter id ");
scanf("%d",&id);
fflush(stdin);
printf("\nEnter name ");
gets(name);
printf("\n[%s] is [%d] years old",name,id);
printf("\nAscii of name = %d",*name);
return 0;
}
I tried printing the string and it prints 0 as the first character's ascii, Why is that. I would have expected that since, I am pressing enter, the string is taken as enter, and the ascii would read 13(ascii of Enter).
Thanks,
Tubby.