Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,133 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,031 people online right now. Registration is fast and FREE... Join Now!




dynamic object creation & naming

 
Reply to this topicStart new topic

dynamic object creation & naming

mesnomer
post 7 Oct, 2008 - 07:37 PM
Post #1


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 1

Part of a project I'm working on involves creating a variable number of objects and naming them incrementally. More specifically, the user should enter an integer and the program should create the specified number of queues, named queue1, queue2, queue3, etc. The following is a sample bit of code:

CODE

cout << "How many queues should be created? ";
int howMany;
cin >> howMany;

for (int i = 0; i < howMany; i++)
{
    createQueue();
}


My problem is not so much with creating the queues, but naming them. I can't seem to figure out how to append a number to a string in order to form a variable name.

Any help would be appreciated. Thanks.
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 7 Oct, 2008 - 09:24 PM
Post #2


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


Well, you can't name variables dynamically. but why don't you think of having dynamic memory allocation and using them as array? that also looks similar to what you are saying.
See, you said queue1, queue2, queue3 and I say queue[0],queue[1],queue[2].

I hope this will help you. smile.gif

This post has been edited by AmitTheInfinity: 7 Oct, 2008 - 09:24 PM
User is offlineProfile CardPM

Go to the top of the page

skaoth
post 7 Oct, 2008 - 10:16 PM
Post #3


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 337



Thanked 9 times

Dream Kudos: 100
My Contributions


QUOTE(mesnomer @ 7 Oct, 2008 - 08:37 PM) *

I can't seem to figure out how to append a number to a string in order to form a variable name.


This is where stringstream is helpful
CODE

#include <sstream>
using namespace std;

...
std::stringstream  val;
val << 5 << "- name";
std::cout << val.str();




User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 11:59AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month