Thread: Scanning in text into a structure

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    5

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2018
    Posts
    5
    Hi Laserlight, The maximum number should be nine, and the maximum lengh should be 10

  4. #4
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    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. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanning Data into a structure from a File
    By Tha_Rappa in forum C Programming
    Replies: 2
    Last Post: 12-08-2006, 12:40 AM
  2. scanning text files
    By deleeuw in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2002, 03:18 PM
  3. Text file scanning
    By spyderwb in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2002, 03:31 PM
  4. Scanning a text file.
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2001, 05:27 AM

Tags for this Thread