Crash whenever I insert value to the vector.

I found how to insert a value at chosen location but instead of deleting, I want it to push down. How do I make this work?

Thank you.

This is the part that doesn't work.
1
2
3
4
5
6
7
8
9
10
11
12
13
            case 2:

                system("cls");
                cout << numberVector.size()-1 << " elements in the vector.";
                cout << "\nEnter an Index Location to add a new value (0 - " << numberVector.size()-1 << "): " << flush;
                cin >> indexNumber;
                cout << "\nEnter a number to add: " << flush;
                cin >> number;
                numberVector.insert(numberVector.begin() +indexNumber + numberVector.size(), number);

                cout << "\nPress any key to continue. ";
                getch();
                break;


rest of code before what doesn't work.

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
58
59
60
61
62
63
#include<iostream>
#include<cstdlib>
#include<conio.h>
#include<vector>
#include<stdlib.h>
#include<time.h>


using namespace std;

int main()
{
    int option, num, numberToFind, found, number,indexNumber;
    int initialSize = 10;
    vector<int> numberVector;
    bool repeat;

    srand(time(NULL));
    for (int i=1; i <= initialSize; i++)
    {
        num = rand()%100+1;
        numberVector.push_back(num);
    }

    do
    {

        system("cls");
        cout << "Select one of the following options: " << endl;
        cout << "\n\t1 - Display vector contents " << endl;
        cout << "\t2 - Add value to vector " << endl;
        cout << "\t3 - Find Value in vector " << endl;
        cout << "\t4 - Erase a value in vector " << endl;
        cout << "\t5 - Quit " << endl;
        cout << "\n\tEnter your choice: " << flush;
        cin >> option;

        switch(option)
        {
            case 1:
                system("cls");
                cout << "Displaying vector contents: \n" << endl;
                for(int j = 0; j < numberVector.size(); j++)
                {
                    cout << "Index # " << j << "\t" << numberVector[j] << endl;
                }
                cout << "\nPress any key to continue. ";
                getch();
                break;

            case 2:

                system("cls");
                cout << numberVector.size()-1 << " elements in the vector.";
                cout << "\nEnter an Index Location to add a new value (0 - " << numberVector.size()-1 << "): " << flush;
                cin >> indexNumber;
                cout << "\nEnter a number to add: " << flush;
                cin >> number;
                numberVector.insert(numberVector.begin() +indexNumber + numberVector.size(), number);

                cout << "\nPress any key to continue. ";
                getch();
                break;
1
2
3
// numberVector.insert(numberVector.begin() +indexNumber + numberVector.size(), number);
if( indexNumber >= 0 && indexNumber <= numberVector.size() )
    numberVector.insert( numberVector.begin()+indexNumber, number );
Topic archived. No new replies allowed.