linked-lists and displaying saved integers...

Associate
Joined
27 Dec 2004
Posts
612
Location
Andover
There is probably a simple answer to this but i'm having trouble displaying what i've input into a record (using linked lists). To start at the beginning, i'm designing a simple program that uses a single linked list (slightly pointless in my opinion but its what i'm being asked to do) that saves only 1 record...
Code:
typedef struct
{
  char name[36];
  unsigned int age[3];
  char gender[2];
  char department[20];
  char modules[10];
  unsigned int measure[2];
} Lecturer;

For example, i read in age...

Code:
printf("Age: \n");
scanf("%d", current->person.age);

But when i come to display the age again, it gives me a 6 digit number.

Code:
 printf("Age: %d \n", lecturer->person.age);

Is the number i'm seeing a memory reference? If so, how can i get it to output the right result? If not, where am i going wrong?
 
Mr Sniper said:
There is probably a simple answer to this but i'm having trouble displaying what i've input into a record (using linked lists). To start at the beginning, i'm designing a simple program that uses a single linked list (slightly pointless in my opinion but its what i'm being asked to do) that saves only 1 record...
Code:
typedef struct
{
  char name[36];
  unsigned int age[3];
  char gender[2];
  char department[20];
  char modules[10];
  unsigned int measure[2];
} Lecturer;

For example, i read in age...

Code:
printf("Age: \n");
scanf("%d", current->person.age);

But when i come to display the age again, it gives me a 6 digit number.

Code:
 printf("Age: %d \n", lecturer->person.age);

Is the number i'm seeing a memory reference? If so, how can i get it to output the right result? If not, where am i going wrong?

The parameters in scanf should be pointers to the memory locations that you wish the data to be placed into.

In effect the unsigned int that is age (Why is it an array btw? - does a person have more than one age?) is being used as a pointer.

What you need is &(current->person.age). The & is the address-of operator.
 
if person.age is an unsigned int array then try person.age[0] instead...

I'm not sure why you would want an array to store a person's age though...

Paul
 
thanks for the replies guys. It was actually a mistake, a friend of mine said to use it when i originally had errors with saving to age. Everything now saves properly.

Next problem... when reading into current->person.name it doesn't like spaces and causes the rest of the record to skip. E.g:
Name: Bob Timpson
Age: <skips>
Gender: <skips>
Department: ....
etc

Is there something wrong with my record or is it just C being a bit silly?
 
Mr Sniper said:
thanks for the replies guys. It was actually a mistake, a friend of mine said to use it when i originally had errors with saving to age. Everything now saves properly.

Next problem... when reading into current->person.name it doesn't like spaces and causes the rest of the record to skip. E.g:
Name: Bob Timpson
Age: <skips>
Gender: <skips>
Department: ....
etc

Is there something wrong with my record or is it just C being a bit silly?

The space is acting as a delimiter.
 
How do i stop it begin a delimiter? For a quick fix i've just made first name and second name and when you put a space inbetween it reads to each pointer.
 
Back
Top Bottom