I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
Index » Programming » C/C++ »

not getting expected result with fgets

shiguera\′s Photo
7 Oct 12, 4:08PM
(1 reply)
not getting expected result with fgets
Hi, I have the following code which I cannot get to work correctly. The first printf doesn't show until I enter the name and hit Enter. What am I missing/doing wrong? I'm using Eclipse...

#include <stdio.h>
 
enum {max_char = 20 };
static char name[max_char+1]="";
 
int main( int argc, char ** argv ) {
	printf("Type your name: ");
	fgets(name, max_char, stdin);
	printf("Your name is %s\n", name);
	return 0;
}

I type the name (daniel) on a blank screen and then the result is printed:

daniel

Type your name: Your name is daniel

Thanks for any help you can provide.
shiguera\′s Photo
8 Oct 12, 4:18AM
Thanks Will, I had tried the first option and did not work either. I tried the scanf as you suggested, with same results. Still a blank line at the beginning.
Currently you need to be logged in to leave a message.