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.
Printable View
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.
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.
I don't know what you mean by "parse it"?
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...
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.
> 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. :)Quote:
Originally Posted by Salem
Perhaps this?Quote:
Originally Posted by jmass16
http://c-faq.com/struct/taggedunion.html
BTW, shun scanf.