Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 10-24-2004, 10:36 PM   #1 (permalink)
 
Super Techie

Join Date: Jun 2004

Posts: 348

sithspawn

Default c++ problem

Hey people,

I am writing a uni program and have encountered a bit of a problem. Ill set you up with a basis of what i am trying to do.

I am writing a simulator for a toll booth system. In this i am using a queue abstract to simulate the queues at the toll booth. The number of queues needs to be specified at run time. I am wondering if there is a way i can attatch a variable to the end of a name to make each queue unique.

Basically i want to do something like this:

for (int i = 1; i <= numQueues; i++)
{
QUEUE que&i;
que&i = createQueue();
}

The bit i am asking about is the &i part. i know this isnt the correct way to code this, if there is a correct way. But if anyone can give me an idea of what to do, it would be VERY helpful.
sithspawn is offline  
Old 10-25-2004, 09:45 PM   #2 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

Use an array of queues. That way you can just refer to them by their index. I haven't used C++ in a while, in C# you'd use an ArrayList (an array that can have a variable number of elements), so I'm not sure if C++ has something similar.

Just add a new "toll booth" to the array for every toll booth you need.
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 10-25-2004, 10:12 PM   #3 (permalink)
 
Super Techie

Join Date: Jun 2004

Posts: 348

sithspawn

Default

yeah i was thinking about doing that. im not too clued up on arrays so i was hoping to be able to avoid that lol.

thanks for the tip
sithspawn is offline  
Old 10-25-2004, 10:39 PM   #4 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

It's pretty simple. In C# (I know you're using C++, but i'm not too keen on that) You'd make the arraylist then just do something like:
Code:
ArrayList arrList = new ArrayList(10);
for(int i = 0; i < 10; i++){
arrList.Add(new Queue());
}
// then you'd access it like this
arrList[1].SomeMethod(); // do stuff with the second queue (since it's zero based)

arrList[4].SomeOtherMethod(); // do more stuff with the 5th queue
//etc
That's the basic idea.
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 10-25-2004, 11:08 PM   #5 (permalink)
 
Super Techie

Join Date: Jun 2004

Posts: 348

sithspawn

Default

thanks for that.

I ended up just biting the bullet and going for the array. its a whole lot neater. Instead of being full of if statements, its full of for statements lol.

thanks for the help works perfectly now
sithspawn is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On