# Thread: Scanning in text into a structure

1. ## Scanning in text into a structure

Hi,

I have an unknown amount of lines, such as the following text:

dog 2 4 2 biscuits wet

cat 5 4 4 biscuits wet dry whiskers

bird 1 2 7 birdseed peanuts bread oats worms insects crackers

I need to feed this into a struct, such as

Code:
```struct animal
{
char animalName[10];
int age;
int numberOfLegs;
int favoriteFoodAmount;
*Some way to store favorite foods*
}```

The last number defines how many favorite foods there are.
I need to be able to store favorite foods. I assume the way to do this would be via an array of char arrays but I have no idea how to do this, or how to implement reading in that text.

Could anyone please give me some pointers on how to do this?

Thanks
Daniel Colthart

2. Is there is known, sufficiently small, maximum number of favourite foods? If so, use a fixed size array of strings. If not, my pointer would be to use a pointer for a dynamic array of strings

A similiar approach applies for the strings themselves: is there a known maximum length of a favourite food name?

3. Hi Laserlight, The maximum number should be nine, and the maximum lengh should be 10

4. That's easy:
The cat eat the bird and the dog eat the cat - rest fifteen through eight is: Your dog have enough for next two days, then You need a new bird and a new cat . . . and so on.
Where is the problem?

I really don't know what You want there with a C-struct.

5. Originally Posted by DcoltGaming
The maximum number should be nine, and the maximum lengh should be 10
So you need to declare a member like this:
Code:
`char favoriteFoods[9][11];`
Your favoriteFoodAmount already keeps track of how many elements of this array are in use.

Then, it is a matter if reading in field by field, then using a loop for the favourite foods.

Popular pages Recent additions