Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cabbit

macrumors 68020
Original poster
Jan 30, 2006
2,128
1
Scotland
Code:
	// Get username
	printf("What was your name again? ");
	
	// Get username using fgets, this is how to do it without a security error
    fgets(name, 15, stdin);
	// End get username
	
	// The user hears someone calling their name, using variable called name
	printf("Mysterious Voice %s %s \n", name, name);

My expected output is "Mysterious Voice name name", my actual output is "Mysterious Voice name
name

"

Anyone know why and how to fix it. name is a char
 
...more precisely, it's coming from the user input. fgets() gives you everything the user inputs up to and including the \n. like lee said, strip it, and you're golden.
 
i fixed it as you suggested using
Code:
// Remove \n from name
	len = strlen(name);
	if( name[len-1] == '\n' )
		name[len-1] = 0;

I was using gets() before which did not have the same problem.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.