Code being skipped

B

Bill

Hi,
My program is skipping over the prompt to enter a first name on the
second pass thru. Any help would be appreciated:

#include <stdio.h>
#include <string.h>


#define first_name_len 30
#define last_name_len 50


void strip_newline( char *str, int size )
{
int i;


/* remove the null terminator */
for ( i = 0; i < size; ++i )
{
if ( str == '\n' )
{
str = '\0';


/* we're done, so just exit the function by returning */
return;
}
}
/* if we get all the way to here, there must not have been a
newline! */



}


int main()
{
char name[50];
char lastname[50];
char fullname[100]; /* Big enough to hold both name and lastname */

char cmd;

do{
printf( "Please enter your name: " );
fgets( name, 50, stdin );


/* see definition above */
strip_newline( name, 50 );


/* strcmp returns zero when the two strings are equal */
if ( strcmp ( name, "Bill" ) == 0 )
{
printf( "That's a cool name.\n" );
}
else
{
printf( "That's not my name.\n" );
}
// Find the length of your name
printf( "Your name is %d letters long", strlen ( name ) );
printf("\n");
printf( "Enter your last name: " );
fgets( lastname, 50, stdin );
strip_newline( lastname, 50 );
fullname[0] = '\0';
/* strcat will look for the \0 and add the second string starting
at
that location */
strcat( fullname, name ); /* Copy name into full name */
strcat( fullname, " " ); /* Separate the names by a space */
strcat( fullname, lastname ); /* Copy lastname onto the end of
fullname */
printf( "Your full name is %s\n\n",fullname );


printf("Enter c to continue, or q to quit");


cmd = getchar();
name[0] = '\0';
lastname[0] = '\0';
fullname[0] = '\0';


}while(cmd == 'c');


return 0;
 
G

Guest

Bill said:
Hi,
My program is skipping over the prompt to enter a first name on the
second pass thru. Any help would be appreciated:
[...]
do{ [...]
printf("Enter c to continue, or q to quit");


cmd = getchar();
name[0] = '\0';
lastname[0] = '\0';
fullname[0] = '\0';


}while(cmd == 'c');

What do you type, key for key, when you wish to continue?
 
M

Mike Wahler

Bill said:
Hi,
My program is skipping over the prompt to enter a first name on the
second pass thru. Any help would be appreciated:

Please see my reply at 'alt.comp.lang.learn.c-c++'

In the future, if you want to post the same message
to more than one newsgroup, don't send it separately
to each group, cross-post instead. Thank you.

-Mike
 
B

Bill

c and ENTER, which goes into the first name variable. I tried fgets,
but then the program just executes once and quits.
Bill said:
Hi,
My program is skipping over the prompt to enter a first name on the
second pass thru. Any help would be appreciated:
[...]
do{ [...]
printf("Enter c to continue, or q to quit");


cmd = getchar();
name[0] = '\0';
lastname[0] = '\0';
fullname[0] = '\0';


}while(cmd == 'c');

What do you type, key for key, when you wish to continue?
 
G

Guest

Bill wrote:

Please don't top-post. I changed it here.
Harald said:
Bill said:
Hi,
My program is skipping over the prompt to enter a first name on the
second pass thru. Any help would be appreciated:
[...]
do{ [...]
printf("Enter c to continue, or q to quit");


cmd = getchar();
name[0] = '\0';
lastname[0] = '\0';
fullname[0] = '\0';


}while(cmd == 'c');

What do you type, key for key, when you wish to continue?

c and ENTER, which goes into the first name variable. I tried fgets,
but then the program just executes once and quits.

I hoped that your answer would be enough for you to figure it out
yourself. getchar() reads a single character. You enter two characters.
The second character remains unread as you restart the loop.
 
S

Simon Biber

Bill said:
c and ENTER, which goes into the first name variable. I tried fgets,
but then the program just executes once and quits.

Call getchar twice. The first time will get the 'c', the second time
will get the '\n' (ENTER).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top