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.
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.
Sent from my iPadŽ
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...
Sent from my iPadŽ
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.
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.
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.Originally Posted by Salem
Sent from my iPadŽ
Perhaps this?Originally Posted by jmass16
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.*