array full of different types

This is a discussion on array full of different types within the C Programming forums, part of the General Programming Boards category; I am currently working on a program that passes an array. I need to enter values into the array first. ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    9

    array full of different types

    I am currently working on a program that passes an array. I need to enter values into the array first. So if the stdin (scanf) is child 5 6, then i want to store in array[0]=child, in array[1]=5, and in array[2]=6. Can someone please help.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Store it all as strings and parse it when you want to access it... that can be very not fun. C or C++ is a bad choice for this program as the languages are heavily typed.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    9
    I don't know what you mean by "parse it"?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    I kind of mixed up a couple of sentences there. You can either, take it all in as a single string. Store it. Parse it when you want the info, or you can parse it as your scan it in, and store them as separate strings based on type. For instance if you had "A. Smith - New York, NY 10108 - 1958", you could store the whole thing... then when you need the info, tokenize, convert to it's appropriate type and use it. Or you can tokenize, store it all into several strings (ie "A.Smith", "New York", "NY", "10108", "1958"), and that will only leave you with the conversion when you need to use the data. It won't be fun, I can tell you that...
    Sent from my iPadŽ

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,669
    To parse text means to break it up into smaller parts. With your problem, you would search the string for "child", and then something that looks like "5", and "6" and then do whatever you want with that information. Say you'd convert the numbers to an int format, for example.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    > then i want to store in array[0]=child, in array[1]=5, and in array[2]=6
    Looks more like a struct containing a string and two integers to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by Salem
    > then i want to store in array[0]=child, in array[1]=5, and in array[2]=6
    Looks more like a struct containing a string and two integers to me.
    Well... I'm not sure he means that will always be the case, but I suppose he didn't really say otherwise. So good catch and good info, I'd say.
    Sent from my iPadŽ

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by jmass16
    I am currently working on a program that passes an array. I need to enter values into the array first. So if the stdin (scanf) is child 5 6, then i want to store in array[0]=child, in array[1]=5, and in array[2]=6. Can someone please help.
    Perhaps this?
    http://c-faq.com/struct/taggedunion.html

    BTW, shun scanf.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 08:51 AM
  2. Having trouble printing from an array
    By Sir Andus in forum C Programming
    Replies: 2
    Last Post: 10-30-2006, 12:48 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21