Add element to array

Hi,
How can I add an element to an array.The array is typeInfo which is my structue type.

I got through a process of asking the user for infor

Something like this .

1
2
3
4
5
int index = size;

cout << " Please enter the first name. " << endl;
cin >> tele[index].fname;


So this process repeats for the info that I need. If I do a cout statement at the end off all this to see if my records have the infor added it does not. Another issue that I was having before was I had a loop that only iterated one time because I was readiing something into and array so I thought I needed a loop. ( I"m unsure of this). But when I did do it that way the information that was added was overwritting the first index's info. So 2 questions Do I still need the loop?
And how do I add properly?
Order is not really that important now I think because the only time when I need it to be alphabetical is in the display listing function which I can sort all of this either within or before.
Thanks for your help.
I would of posted more but I'm at my grandmas so no wifi on the laptop.
Happy thanksgiving:)
closed account (zb0S216C)
Arrays cannot be resized once their size has be specified. Alternatively, you can use "std::vector"[1, vector] which is a resizeable array.

References:
[1, vector]http://www.cplusplus.com/reference/stl/vector/


Wazzak
Last edited on
vector would be you best bet look them up on the internet when you can be sure to include vector like this
#include <vector>
and also it is in the std namespace too
What if the array is of size 10; I have 5 things in it previoisly and I want to add to it one more thing. Then the space would be there. Thats the way it is now.
I can;t use vectors we don't cover those yet.

If I could add a person to the array then how could I do that in a general way so each time that function was called. As long as it is not more then the size of the array I could add someone to it.
It's a phone directory it only has 5 records plus what ever the user adds.
So how can I accomplish this? I thought my code above would but NOPE!
Thanks
Have yall covered the new and delete keyword yet?
closed account (zb0S216C)
Well, technically, the "spaces" within the array are already occupied by a piece of data. However, you could just overwrite the space with user-defined data; given your constraints, you'd have to. If you're working with structures, you could add a Boolean data-member which would indicate whether or not that record is holding useful data. For example:

1
2
3
4
5
6
7
8
9
10
11
struct PhoneRecord
{
   bool InUse_;
};

int main( )
{
    PhoneRecord NewRecord_;
    if( NewRecord_.InUse_ )
        // ...
}

Wazzak
Have yall covered the new and delete keyword yet?
Nope. I don't think I have no clue what that is.

Framework (3105)

I"m working with structures. Is the code I gave as sample not sufficent to add to an array. Do I need the loop? Also where does that leave me if I'm working with structures? If I want to overide a specific index how can that be done.
Thanks
closed account (zb0S216C)
You only need a loop if you're planning on changing multiple records continuously. Because you're working with structures, you're able to incorporate my example code if you wish. In doing so, you're able to distinguish between the records that are in use and those that are not. As for "override a specific index", what do you mean by that exactly?

Wazzak
You said there was crap within in those spots in the array even though they don't have say , first name, last name,..etc.
So If there is still something there I used the term override maybe not a good choice of words. So about the multiple records things the function will on;y add one element when it is called. SO i'm unsure about if I should use the loop or not.

About your example.

If there is something within the array still like I said above then what is bool checking for ?
I'm unsure.
Sorry thanks for help

Just make the array larger than you'll ever need. Also, store the size of the array somewhere else and increment it when you want.

Example:
cin >> tele[sizeof_tele++].fname;

This will let you keep track of the number of used spaces in tele, and will increment as you add more elements.
So I have an idea I will write it .Tell me what you think.

1. Have a loop that goes through the array.
2. Find the spot with no index information.
A |B | C | empty | empty | empty |
3. Set that location = to a variable
4. use that variable as the location to recieve the input.
example tele[index]
Where index is the location found to be empty from the loop.
That way when ever the function is called it adds something to empty location.
My question is this ..Well, technically, the "spaces" within the array are already occupied by a piece of data.. So what am I checking for specifically? I know A, B, C are strings (say) . But what about when I get the empty. If they are not empty actually as stated above what the heck am I seeing is there or not?


So what do you guys think?
Topic archived. No new replies allowed.