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.

Recommended Answers

All 3 Replies

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;

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.

The alternative, using the <map> library, if you'd prefer to give the orders a 'Key' value identifier

#include <map>

/* ... */

std::map< std::string, order > the_orders;
order my_order;
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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.