Noobie C++ questions (arrays / pointers / objects)

Associate
Joined
22 Dec 2004
Posts
1,194
Location
Paderborn DE / TeessideUK
Right i think i need to read in data from keyboard input, set up an object with this data as its private attributes using a constructor. Store the pointer to this object within a vector of pointers. Then moving onto the next data and repeat placing the pointer to this object into the next memory slot within the vector.

Now the problem is that, say i read in 1 set, for example:

int x, y
for ( z = 0; z != -1; z++) //starting for loop
{
cout << "x = ?"; //reading in the variables
cin >> x;
cout << "y = ?";
cin y;

so the starting of a for loop, reading in x and y from the keyboard now these variables are set and so i would create the object for example : "object object1(x,y)" thus setting the variables but i need to read in many sets of data and place their pointers inside the vector.

Is there a way to say use the "z" variable to influence the naming of the object say.. object z-object(x,y). for example is z was 1 then the object would be called 1object or something like that. Otherwise i cant see how i can aswell create pointers to the object to put into the vector as i create them.

Now bare in mind this all may seem rubbish to a programming as i am severly lacking in basic programming knowledge but i may get something useful out of it :).

Ty in advance
Firthy15
 
I'm strugglin to see just what your doing, but I think I understand what your trying to do.

Trying using a linked list to add the items into a list altho everything that is entered into the keyboard and stored in the list would be backwards when you read through the list data.

The other alternative is a hash table which you can then pick out all the data in order.

Anyway soz if this doesn't apply to what your doing and I'm mis-understanding something

Gaunt
 
No you can't specify object names during runtime, I can't see why you'd want or need to do this. If you're going to store a pointer to each object in an array, then you may as well reference them via "arrayOfObjects[1]" etc. Here's some pseudocode of how I understand what you want to do.
Code:
while userInput
    Initialise null object pointer 
    read userinput
    create object
    store pointer in array
endwhile
 
No you can't specify object names during runtime, I can't see why you'd want or need to do this. If you're going to store a pointer to each object in an array, then you may as well reference them via "arrayOfObjects[1]" etc. Here's some pseudocode of how I understand what you want to do.
Code:
while userInput
    Initialise null object pointer 
    read userinput
    create object
    store pointer in array
endwhile

but when coding this don't you need to name each object differently? think im missing some basic, but surely i need something to be able to change the object name each time the loop operates or i will just write over the existing object?

ty for the help i know my explanation was horrible :P :)
 
No you won't need to name them differently because providing you declare the object within the loop, it will be destroyed and reinstantiated with each iteration of the loop.
 
Back
Top Bottom