User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Dec 2007
Posts: 1
Reputation: TimeIsCyclical is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
TimeIsCyclical TimeIsCyclical is offline Offline
Newbie Poster

variables naming variables.

  #1  
Dec 23rd, 2007
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.

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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,337
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 378
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: variables naming variables.

  #2  
Dec 23rd, 2007
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.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,822
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 187
Featured Poster
Duoas's Avatar
Duoas Duoas is online now Online
Posting Virtuoso

Re: variables naming variables.

  #3  
Dec 23rd, 2007
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
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.
Reply With Quote  
Join Date: Feb 2006
Location: UK
Posts: 468
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Rep Power: 5
Solved Threads: 42
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: variables naming variables.

  #4  
Dec 24th, 2007
The alternative, using the <map> library, if you'd prefer to give the orders a 'Key' value identifier
  1. #include <map>
  2.  
  3. /* ... */
  4.  
  5. std::map< std::string, order > the_orders;
  6. order my_order;
  7. the_orders.insert( std::make_pair( "order1234", my_order) );
Although, the vector/deque solution posted by Salem is simpler IMHO.

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?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:06 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC