|
maggotz


Registered: 06/24/06
Posts: 7,539
|
C++
#7418109 - 09/16/07 09:57 PM (16 years, 4 months ago) |
|
|
i need some help from all you programmers out there.
i need to write a program that handles resources for projects, like when to buy materials, how much to buy and stuff like that.
but i ran into a little problem. i need to be able to enter a product, then the materials needed for that product, then for each of those materials i need to enter their respective materials and so on. i need to enter a whole lot more things but after i figure this out i can do that on my own. here's an example:
 for product T i need 2 units of material U and 3 units of material V. Now, for U i need 1 unit of W and 2 units of X. For V i need 2 units of W and 2 units of Y.
so what i had thought of doing was this:
struct product{ char name[20]; .... .... .... // these are a bunch of other variables that i need, don't mind them. int components; // this is the number of materials or components that this particular product or part needs product *parts; // this is were i'll make an array of all the components for this part or product, that's why i need "components" };
so basically, i would ask for the name and a bunch of other data and then i would ask how many components or materials this product needs, and based on this number i would make an array of products/materials/components needed for this part/product.
for T, i need 2 materials so T.components would take the value of two and then i would do this: T.parts = new product[T.components];
that would create an array of products "inside" (you know what i mean) of T, and then i would have to fill up all the members of this array(in the example i gave it would be U and V). so when filling up U and V, it would in turn create new arrays of products/materials for them. but since i don't know how many "levels" of materials there will be i think i need a recursive function to fill up the chain. i was wondering if anyone knew how that function should be cuz i have no idea. i haven't had anything to do with C++(or any other language) in at least three years so i'm more than just a little rusty, i'm fucking lost.
any help would be greatly appreciated.
|
maggotz


Registered: 06/24/06
Posts: 7,539
|
Re: C++ [Re: maggotz]
#7418116 - 09/16/07 09:58 PM (16 years, 4 months ago) |
|
|
was i making myself clear?
|
Jrsxt
Stranger

Registered: 05/14/07
Posts: 1,043
|
Re: C++ [Re: maggotz]
#7418136 - 09/16/07 10:02 PM (16 years, 4 months ago) |
|
|
what kind of business is it
|
maggotz


Registered: 06/24/06
Posts: 7,539
|
Re: C++ [Re: Jrsxt]
#7418196 - 09/16/07 10:17 PM (16 years, 4 months ago) |
|
|
i actually need it for a class.
|
Compass
Ancient Light

Registered: 10/17/06
Posts: 1,149
Loc: The Border of Reality
Last seen: 7 years, 2 months
|
Re: C++ [Re: maggotz]
#7418203 - 09/16/07 10:20 PM (16 years, 4 months ago) |
|
|
if it is C++, maybe use std::string instead of char[20] ?
and instead of an array of products, consider std::vector
but for the recursive part,
Code:
void fill_products(product& prod) {
if more products: allocate array for each product in new array: fill_products(i)
else: // base case return
}
|
maggotz


Registered: 06/24/06
Posts: 7,539
|
Re: C++ [Re: Compass]
#7418237 - 09/16/07 10:28 PM (16 years, 4 months ago) |
|
|
that probably makes more sense but like i said i hadn't seen a line of code in like 3 years and i still haven't checked my book.
i'll try that recursive bit but it'll have to wait till tomorrow. i've has like 5 hours of sleep since friday. thanks for your input.
|
maggotz


Registered: 06/24/06
Posts: 7,539
|
Re: C++ [Re: maggotz]
#7418246 - 09/16/07 10:29 PM (16 years, 4 months ago) |
|
|
could you elaborate on std::vector? what is it and how could i use it?
btw, i'm no pro programmer.
|
Compass
Ancient Light

Registered: 10/17/06
Posts: 1,149
Loc: The Border of Reality
Last seen: 7 years, 2 months
|
Re: C++ [Re: maggotz]
#7418499 - 09/16/07 11:36 PM (16 years, 4 months ago) |
|
|
a vector is like an array, but it keeps track of the size for you and automatically allocates new space when you add new items to the *back* of the array.
it's part of standard C++, so you will always be able to use it. it is part of the std (standard) namespace.
at the top of your code.
#include <vector> using std::vector;
then you can just say
struct product { // .... vector parts < product >; }
if you don't know about namespaces and templates, read up! key to learning C++, instead of just C.
-------------------- nystagmus dopamine guru inverted pop culture love scars of sorrow fleshy synesthesia hippie farts perpetual tinnitus Reclaim the Swastika!
|
SymmetryGroup8
It's about theFLOW!



Registered: 02/25/07
Posts: 506
Last seen: 16 years, 8 days
|
Re: C++ [Re: Compass]
#7418605 - 09/17/07 12:34 AM (16 years, 4 months ago) |
|
|
-------------------- Be like water my friend!
|
Seuss
Error: divide byzero



Registered: 04/27/01
Posts: 23,480
Loc: Caribbean
Last seen: 2 months, 20 days
|
Re: C++ [Re: maggotz]
#7418861 - 09/17/07 05:17 AM (16 years, 4 months ago) |
|
|
It sounds like a very simple 'n-tree' problem with 'prefix traversal' to calculate the result. Google is your friend, now that you know the appropriate search terms.
-------------------- Just another spore in the wind.
|
supra
computerEnthusiast
Registered: 10/26/03
Posts: 6,446
Loc: TEXAS
Last seen: 12 years, 9 months
|
Re: C++ [Re: maggotz]
#7420400 - 09/17/07 04:44 PM (16 years, 4 months ago) |
|
|
take a look here, anytime i need help with certain containers or classes, this is where i look
http://www.sgi.com/tech/stl/
and for vectors, here is a complete list of its member functions and use
http://www.sgi.com/tech/stl/Vector.html
personally, if i was to code this, i would use a switch/case setup to call different functions for each product....but im also no programmer.
And definitely use a string for the name, it is just an array of chars anyways...
peace
|
maggotz


Registered: 06/24/06
Posts: 7,539
|
Re: C++ [Re: supra]
#7422563 - 09/18/07 06:56 AM (16 years, 4 months ago) |
|
|
thank you all for your help. and yeah, i ended up using string instead of the char[20]. i haven't figured it out 100% yet but with all these links i think i'm on the right track now. 
i remember back in the day, when i could have this shit sorted out in no time.
|
Seuss
Error: divide byzero


Registered: 04/27/01
Posts: 23,480
Loc: Caribbean
Last seen: 2 months, 20 days
|
Re: C++ [Re: maggotz]
#7422570 - 09/18/07 07:00 AM (16 years, 4 months ago) |
|
|
> i remember back in the day, when i could have this shit sorted out in no time.
*laugh* I hadn't written a line of C code in years (a while back) and sat down one day to write something... it took a good week or two before I got my skills back... and I am somebody that has literally written several hundred thousand lines of C code.
-------------------- Just another spore in the wind.
|
maggotz


Registered: 06/24/06
Posts: 7,539
|
Re: C++ [Re: Seuss]
#7422587 - 09/18/07 07:08 AM (16 years, 4 months ago) |
|
|
i know how you feel. i had forgotten how fun it was to write a few good lines. am i nerd for saying that?
|
|