1

I'm trying to develop a program that, in case 2, after an array has been "filled" it adds another space so you are able to add another value to your original array. I tried my best but failed miserably. I cannot use std::copy to just copy the array and add it to a new one with more space in it. Any help is appreciated, thanks!

#include <iostream>
#include <string>
using namespace std;


void stampa (string x[], int q){ //Cout all inserted names
    int i;
        
    cout<<"Your names are: "<<endl;
    for (i=0; i<q; i++){
        cout<<x[i]<<endl;
    }
}

void carica(string x[], int q){ //Function to insert the names
    int i;
    
    for(i=0; i<q; i++){
        cout<<"Insert the "<<i+1<<" name:"<<endl;
        cin>>x[i];
 }
}

void controllo (int &x){ //Error if a negative number is inserted
    while (x<=0) {cout<<"Error! Insert a positive number:"<<endl;
    cin>>x;
   }
}


int main(){
    int scelta, n;
    string nome;
    bool exit=false;
    cout<<"How many names do you want to enter?"<<endl;
    cin>>n;
    controllo(n);
    string a[n];
    carica(a,n);
    do{
        system("cls");
        cout<<"1) Print your names"<<endl;
        cout<<"2) Insert a new name"<<endl;
        cout<<"3) Exit"<<endl<<endl;
        cin>>scelta;
        switch (scelta){
            case 1:
                stampa(a,n);
                system("pause");
                break;
            case 2:
                cout << "Enter new name to insert: " << endl;

                cout<<"Done!"<<endl;
                system("pause");
                break;
            case 3:
                exit=true;
                cout<<"Goodbye!"<<endl;
                break;
        }
    }while (!exit);
system("pause");
}
Colem
  • 13
  • 4
  • The solution is to copy the elements to a larger array. Use `std::vector` which does it for you. – François Andrieux Apr 29 '21 at 21:43
  • Are you able to write your own loop to perform the element-by-element copy that `std::copy` is doing under the hood? – Nathan Pierson Apr 29 '21 at 21:43
  • why not make a new array and delete the old one, as it will also save memory, or you can use std::vector – Daniyal Shaikh Apr 29 '21 at 21:43
  • Your going to have to make a copy. Arrays have a fixed size, even the ones allocated with new. Once you want to go bigger then that size, you need to allocate a new, bigger array, and then copy the old array into it, and then add the element. – NathanOliver Apr 29 '21 at 21:44
  • @DaniyalShaikh Because my teacher doesn't want it that way – Colem Apr 29 '21 at 21:44
  • @FrançoisAndrieux Can't do it that way – Colem Apr 29 '21 at 21:44
  • Can you show the assignment question? i dont think u got the question correct – Daniyal Shaikh Apr 29 '21 at 21:46
  • Does this answer your question? [How to add element to C++ array?](https://stackoverflow.com/questions/755835/how-to-add-element-to-c-array) – zoldxk Apr 29 '21 at 21:46
  • @DaniyalShaikh It's not in the actual question it self, but she specifically said we cannot use it when she assigned the assigment – Colem Apr 29 '21 at 21:49
  • @Colem What is the *actual* assignment? What others have said is true, the *only* option is to create a larger array, copy the existing elements to it, and free the old array. `std::vector` handles all of this for you. But if your teacher won't let you do that, even manually, then either the assignment is faulty to begin with, or you are simply misunderstanding it. Chances are, the teacher doesn't want you using `std::vector`, but actually does want you to create a new larger array manually. – Remy Lebeau Apr 29 '21 at 21:53
  • @RemyLebeau Yes! That's exactly it! Sorry, I'm new to programming and didn't know how to explain it well – Colem Apr 29 '21 at 21:57
  • @FrançoisAndrieux `std::realloc()` would not work correctly for an array of non-POD types, like `std::string`. `new[]` (and `delete[]`) is required. – Remy Lebeau Apr 29 '21 at 22:02
  • @RemyLebeau Thanks, I didn't notice the array was of `std::string`. I guess I assumed if `std::vector` was off the table everything would have been trivial types. – François Andrieux Apr 29 '21 at 22:06

1 Answers1

0

Try using std::vector instead of a simple array, or create an array through pointers.

vector: https://www.youtube.com/watch?v=Jh2urtP00Zg
Pointers: https://www.programiz.com/cpp-programming/memory-management

Tim
  • 25
  • 4