0

In this code I'm inserting element to an array using insertion algorithm.First,I insert element in an array.Then I use while loop if user enter 2, a new element will be added to the array,So user have to enter the location and the element. "length" variable is for updating the size of array

#include<iostream>
using namespace std;

int main(){
    int sizee;
cout<<"how many names you want to enter:"<<endl;
cin>> sizee;
string name[sizee];



int length = sizee;
cout<<"Enter name: "<<endl;

for(int i=0;i<sizee;i++){
    cin>>name[i];
}

while(true){

cout<<"Press 1 for check list"<<endl;
cout<<"Press 2 for insertion "<<endl;
cout<<"press 4 for exit"<<endl;

int choice;
cin>>choice;

if(choice==1){

    for(int i= 0;i<length;i++){
    cout<<name[i] <<" ";
}
cout<<""<<endl;
}
else if(choice==2){

cout<<"Please enter location(starts from 0): "<<endl;
int loc;
cin>>loc;
cout<<"Please enter name: "<<endl;
string update;
cin>>update;
length = length + 1 ;
name[length];
for(int i = length-2;i>=loc;i--){
    name[i+1] = name[i];
}
name[loc] = update;
}
else if(choice==4){
    break;
}
}
return 0;
}

Sample I/O:

how many names you want to enter:
2
Enter name:
James Sam

Press 1 for check list
Press 2 for insertion
press 4 for exit
2
Please enter location(starts from 0):
1
Please enter name:
Lucas

Press 1 for check list
Press 2 for insertion
press 4 for exit
1
James Lucas Sam

Press 1 for check list
Press 2 for insertion
press 4 for exit

My code is working for the first iteration. But when I again enter 2 for inserting new element a error occurs.

2
  • 1
    string name[sizee] is not valid c++, you should use a std::vector instead (which would also allow you to not have to ask the number of names in advance) Commented Jan 29, 2020 at 17:20
  • I presume name[length] is supposed to resize the array? It doesn't do anything Commented Jan 29, 2020 at 17:21

1 Answer 1

2

A couple things here...

First off, this is invalid C++ code:

string name[sizee];

If you're creating an array, it must be with a constant size.

Secondly, when inserting you do this:

name[length];

It looks like you may expect that to resize your array? However, array sizes are constant. Once you create an array, its size cannot be changed. So this line actually tries to access length element of name (which is out of range) and then do nothing with it. Your following loop assumes that the size of the array has increased and further goes out of range.

So how do you fix this? Stop using arrays! They'll only cause you pain. Instead, I'd suggest an std::vector (or even better if this is your full program, an std::list).

That'd look like:

cin>> sz;
std::vector<std::string> name(sz);

// ...

else if(choice==2){
    std::cout<< "Please enter location(starts from 0): "<< std::endl;
    int loc;
    cin>>loc;

    cout<<"Please enter name: "<<endl;
    string update;
    cin>>update;

    name.insert(name.begin() + loc, update);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Why std::list better? Thats rarely the case, e.g. see modernescpp.com/index.php/…
And this is one of those rare cases @Alan. All OP is doing is iterating over the structure (same cost for vector/list) and inserting into the structure (which'll be a little faster for a list).
even in this simple case vector may be faster, its only holding std::string so only a few bytes per element so the reallocation cost is small, the bookkeeping overhead of std::list may be higher. if the strings are small there is the added benefit of the small string optimisation meaning the strings are stored in the vector itself as a single contiguous block of memory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.