Hi, all I'm trying to do is have my program request a name then after name is inputted, ask if there are any more names to add [Y/N].

Problem is program keeps displaying 'add name' and 'any more names to add' on the same line without giving the option to add a name only to input Y/N! Yes, I'm a newbie... it figures...

Have spent hours tweaking these few lines of code and am now at my wits end. Any suggestions would be greatly appreciated.

int f_enter_name ()
{
/* local variables */
char reply;
float count = 0;
int num_of_friends = 0;
 
do
{
printf ( "Please enter the name to add to record %d > ", num_of_friends + 1 );
gets ( friend_info[num_of_friends].name );
fflush (0);
 
do
{
printf ( "Are there any more names to add? Y/N > " );
scanf ( "%c", &reply );
fflush (0);
 
if (( reply != 'Y' ) && ( reply != 'y' ) && ( reply != 'N' ) && ( reply != 'n' ))
{
printf ( "Error. Y or N only\n" );
}
}
while (( reply != 'Y' ) && ( reply != 'y' ) && ( reply != 'N' ) && ( reply != 'n' ));
 
num_of_friends ++;
 
count = count + 1;
 
}
while (( reply == 'Y' ) || ( reply == 'y' ) && ( num_of_friends < 20 ));
return ( count ); 
}

Thanks for looking!

The problem is when you do scanf ( "%c", &reply ); you press two keys, a character and ENTER. The scanf() reads the character and leaves the ENTER for the next read. You need to clean out the input buffer (and fflush() is not the way to do it). You can use a loop that reads the input buffer until it reaches '\n' at which point it exits. That way you can enter YES, YEAH, YOU BETCHA and the read still works fine.

And be sure to read the links Salem posted.

Comments
Good ~~SpS
This article has been dead for over six months. Start a new discussion instead.