I am extremely new to working in C so forgive me if I have just made a simple error but I am extremely confused. My code is as follow:
#include <stdio.h>
#include <string.h>
int
main ()
{
char first[10],last[10],address[20],city[10],state[3],zipcode[5];
printf("\n\nPart 3:\n");
printf("Please enter your name and address:\n");
printf("First Name: ");
scanf("%s", first);
printf("Last Name: ");
scanf("%s", last);
printf("Address: ");
fgetc(stdin);
fgets(address, 20, stdin);
address[strcspn(address,"[/r/n]")] = 0;
printf("City: ");
scanf("%s", city);
printf("State: ");
scanf("%s", state);
printf("Zipcode: ");
scanf("%s", zipcode);
printf("%s %s\n%s%s, %s, %s",first,last,address,city,state,zipcode);
return 0;
}
Everything works except when the final print statement is read first is not output, it just begins with last name. Example:
For the input
Please enter your name and address:
First Name: Ben
Last Name: Johnson
Address: 1234 Smith St
City: Townsville
State: CA
Zipcode: 12345
I get the following output:
Johnson
1234 Smith St
Townsville, CA, 12345
Just not sure where it is going wrong as my first is being inputted the exact same way as my other variables that are working.