Structure Problem

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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
    
that's not a problem af the structure.

you simply use it the wrong way.
On line 18: ID is an array not an int
On line 22: gender is a char not an int

On line 32 to 34: Not a string

Look here:
http://cplusplus.com/reference/cstdio/printf/?kw=printf
what's the right format for printf/scanf
ok thanks man
this hasn't seem to fix the problem, when i run the code it still skips the entry of the gender? any suggestions as to why that's happening?
Can you post your updated code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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" ?
Last edited on
line 22 and 39 is still wrong: replace %s with %d (since it's int)
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) 
scanf returns a value, so you should always test this to see how it went, before using the value it read in.

http://www.cplusplus.com/reference/cstdio/scanf/


Same for all the other functions that return a value - if you want to be pedantic, you could apply this to printf as well.
Last edited on
thanks a million for all the help got it sorted!
Topic archived. No new replies allowed.