When I run this it jumps past the "Enter first name" and goes straight to "Enter last name".
Example:
Enter first name: Enter last name: Frank
The name is Frank
Enter Phone:
It won't let me put the First Name in. Ideas?
if( (fp=fopen("contacts","w")) == NULL )
{
printf( "Failed to open file contacts to write.\n" );
exit( 1 );
}
printf("Enter first name: ");
fgets(first, sizeof(first), stdin);
first[strlen(first) - 1] = '\0';
printf("Enter last name: ");
fgets(last, sizeof(last), stdin);
last[strlen(last) - 1] = '\0';
strcpy(list.name, first);
strcat(list.name, " ");
strcat(list.name, last);
printf("The name is %s\n", list.name);
printf("Enter Phone:");
fgets( line, sizeof( line ), stdin );
sscanf( line, "%s",&list.ph);
printf("You entered: %s", &list.ph);
printf("Enter Address:");
fgets( line, sizeof( line ), stdin );
sscanf( line, "%s", list.add );
printf("Enter Email Address:");
fgets( line, sizeof( line ), stdin );
sscanf( line, "%s", list.email );
printf("\n");
fprintf( fp, "%s\t%s\t%s\t%s", list.name, &list.ph, list.add, list.email );
fclose(fp);
I suspect you declared "first" as a char pointer, you must declare it as an array if you want that sizeof works otherwise you get the size of the pointer, regardless where it points
char first[100];
printf("Enter first name: ");
fgets(first, sizeof(first), stdin);
disclaimer: you didn't show the decl of any variables
There probably is some data (a wild \\n
, for example) in stdin. It is better to run your code under a debugger and see what happens.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.