i am writing a struct and it doesn't seem to be working. Can anyone help? the code is meant to read in the details of a person and simple print out the results, but it skips the entering of the first name and then it doesn't print out the input. Should i be using puts to print out the strings instead of printf? Any help will be great thanks.
#include <stdio.h>
#include <string.h>
struct customer
{
int ID [20];
int age;
char gender;
char name [40];
char surname [40];
char address [100];
};//end to customer sturucture
main()
{
struct customer new_customer;
printf("Please enter your ID. \n");
scanf("%d", & new_customer.ID);
printf("Please enter your age. \n");
scanf("%d", & new_customer.age);
printf("Please enter your gender. ( m or f ) \n");
scanf("%d", &new_customer.gender);
printf("Please enter your firstname. \n");
gets(new_customer.name);
printf("Please enter your surname. \n");
gets(new_customer.surname);
printf("Please enter your address. \n");
gets(new_customer.address);
//printing results
printf("Your ID is %s \n", new_customer.ID);
printf("Your age is %s \n", new_customer.age);
printf("Your gender is %s \n", new_customer.gender);
printf("Your firstname is %s \n", new_customer.name);
printf("Your surname is %s \n", new_customer.surname);
printf("Your address is %s \n", new_customer.address);
flushall();
getchar();
}//end to main
#include <stdio.h>
#include <string.h>
struct customer
{
int ID;
int age;
char gender;
char name [40];
char surname [40];
char address [100];
};//end to customer sturucture
main()
{
struct customer new_customer;
printf("Please enter your ID. \n");
scanf("%d", &new_customer.ID);
printf("Please enter your age. \n");
scanf("%s", &new_customer.age);
printf("Please enter your gender. ( m or f ) \n");
scanf("%c", &new_customer.gender);
printf("Please enter your firstname. \n");
gets(new_customer.name);
printf("Please enter your surname. \n");
gets(new_customer.surname);
printf("Please enter your address. \n");
gets(new_customer.address);
//printing results
printf("Your ID is %d \n", new_customer.ID);
printf("Your age is %s \n", new_customer.age);
printf("Your gender is %c \n", new_customer.gender);
printf("Your firstname is %s \n", new_customer.name);
printf("Your surname is %s \n", new_customer.surname);
printf("Your address is %s \n", new_customer.address);
flushall();
getchar();
}//end to main
So when you say it skips the entry of the gender - do you mean that it outputs "Please enter your age." and waits for user input, and then immediately outputs "Please enter your firstname. \n" ?
sorry i uploaded the wrong one, i hvae another version changed, What it does it prints out as it should but it skips the scanf for entering the gender, it still prints out 'please enter your gender' but doesnt allow the user to input an answer
the problem is that scanf("%d") leaves the end of line character in the stream. scanf("%c") for gender takes the end of line character.
to prevent that problem do this:
1 2 3 4 5
printf("Please enter your age. \n");
scanf("%d%*c", &new_customer.age); // %*c removes the end of line '\n' char
printf("Please enter your gender. ( m or f ) \n");
scanf("%c%*c", &new_customer.gender); // remove '\n' for the following gets (otherwise it would skip too)