•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,515 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,671 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 370 | Replies: 3
![]() |
•
•
Join Date: Dec 2007
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
I'm creating a billing program for my college's print shop. I'm doing this with a class called "orders" that signifies each new order that is taken. I'm having trouble figuing out how to create new variables of that class without prompting the user to input anything though... After the previous order is finished I want the program to automatically set up for a new order by creating a new variable of type "orders". My problem is figuring out how to make this happen. Here is what I have so far.
So, now i have a nice string variable with a concatenated number to create "order1", "order2" etc. But now I need to use that variable to name a variable of type "orders".
I've heard this can be done with mapping somehow, but I'm not sure how that works at all.
orderNumber++; //initialized to zero, type int// std::string index = "order" + stringify(orderNumber); // 'stringify' is a function that converts an integer to a string
So, now i have a nice string variable with a concatenated number to create "order1", "order2" etc. But now I need to use that variable to name a variable of type "orders".
I've heard this can be done with mapping somehow, but I'm not sure how that works at all.
Last edited by TimeIsCyclical : Dec 23rd, 2007 at 1:39 pm.
orders array_of_orders[MAX_ORDERS];Which you index with something like
array_of_orders[orderNumber].member = something; orderNumber++;
Or for something more C++,
std::vector< orders > vector_of_orders; If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,822
Reputation:
Rep Power: 11
Solved Threads: 187
Yes, please, do it the C++ way.
I think that you might expect a large number of orders to be stored? In which case a deque is a better choice than a vector.
And like Salem indicated, there is no need to create named variables (order1, order2, etc.), just use the deque or vector or whatever you choose so you can say
etc.
where 0, 1, 2, etc is easily kept in a variable (like "orderNumber") that can be manipulated as needed.
Hope this helps.
I think that you might expect a large number of orders to be stored? In which case a deque is a better choice than a vector.
And like Salem indicated, there is no need to create named variables (order1, order2, etc.), just use the deque or vector or whatever you choose so you can say
vector_of_orders[ 0 ]vector_of_orders[ 1 ]etc.
where 0, 1, 2, etc is easily kept in a variable (like "orderNumber") that can be manipulated as needed.
Hope this helps.
The alternative, using the <map> library, if you'd prefer to give the orders a 'Key' value identifier
Although, the vector/deque solution posted by Salem is simpler IMHO.
A little more on maps -
http://www.cppreference.com/cppmap/index.html
CPP Syntax (Toggle Plain Text)
#include <map> /* ... */ std::map< std::string, order > the_orders; order my_order; the_orders.insert( std::make_pair( "order1234", my_order) );
A little more on maps -
http://www.cppreference.com/cppmap/index.html
Last edited by Bench : Dec 24th, 2007 at 11:29 am.
¿umop apisdn upside down? ![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Array (C++)
- sry for asking this but... (VB.NET)
- Gaussian elimination in pascal (Pascal and Delphi)
- little shell scripting help here... (Shell Scripting)
- help with programming (C)
- som help... (C++)
- YaBasic: The beginners choice (Computer Science and Software Design)
- C++ Syntax (C++)
Other Threads in the C++ Forum
- Previous Thread: Plz Help If You Can
- Next Thread: Objects Appears to get Destroyed..Please Help!



Linear Mode