How do I add elements to an array?

I've a file that looks like this:

84484-37.96-Castor, Kathy
39050-69.68-Chandler, Ben
26183-70.84-Costello, Jerry

I have successfully read each element
the id, grade and name into 3 separate array.
Now i need to add a new student with an id and grade

How do i do this?

This is what I have.
Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
int addStudent( int Iarray[], double dArray[], string sArray[], int newID, 
     double newGrade, string newName, int size )
 {
  char ready;
  int index;

  cout << endl;
  cout << "Enter new student ID number     : ";
  cin >> newID;
   
  for( index = 0; index < size; index++ )
   {
    if( newID != Iarray[index] )
      {
        newID += Iarray[index];
        //Iarray[index] = newID;
          
      }
    else
      {
        cout << endl;
        cout << "ERROR: Duplicate student ID found, Add Student Module aborted" 
               << endl;
        cout << endl;
        return 0;
      }
     
   cout << "Enter student name <last, first>: ";
   cin >> newName;
   
   cout << "Enter student grade             : ";
   cin >> newGrade;

   cout << endl << endl;

   cout << "Complete student addition process <y/n>: ";
   cin >> ready;

   
   for( index = 0; index < size; index++ )
     {
      if(toUpperCaseLetter(ready) == 'Y' )
       {
       
        sArray[ index ] = newName;
        dArray[ index ] = newGrade;
       }
       else
        return 0;
       }
        

       

     
   
 }
An array can not change size; you can not add elements to an array.

However, a std::vector can change size, and you can add elements to it.
http://www.cplusplus.com/vector
Also, it is proper to teach/learn vectors before arrays (arrays are much more difficult)
Last edited on
The only way to change the size of an array is to make it dynamic, and have it so that when you want to increase the size, you create the second array, transfer all values, and then delete the first array. Obviously, this method requires much more code and is far less efficient than just using vectors, which are built specifically for the problem you are having to deal with here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//take in an array of values, and return a new array with a new value
template <class Utype>
Utype extend_arr(Utype arr[], int length, Utype* val)
{
  Utype array2 = new Utype[length + 1];//create the new array
    for (int J = 0; J < length; J++)
      array2[J] = arr[J];//fill it with the values from the previous array
    
    array2[length] = val;//attach the new value to the end of it
    
    delete[] arr;
    arr = NULL;
  
   return array2;//return the array
}
@Smac89 your code is incorrect.
-extend_arr's return type should be a pointer, currently it can only return an element of the array, which makes line 14 invalid.
-val should be passed by const reference, currently passing by pointer makes line 9 incorrect.
-Setting arr to NULL on line 12 does not affect arr outside the function.

Really, arr should be a pointer passed by reference and extend_arr should return void - the function should modify what the pointer points to so there is no risk of dangling pointers.

And honestly, this much code versus using a vector only means room for mistakes (such as the mistakes you demonstrated).
Topic archived. No new replies allowed.