My program runs fine. The only problem is at the bottom of my "insertAt" function. I am trying to insert a number to the array. Whatever number I insert, it keeps appearing at the front of the array instead of the position I want it to.

For example:


Original Array:

23,65,34,82,37,12,17,24,36,82,51

user input: 5 2 (with 5 as the number and 2 as in the position in the array)


comes out as:

5,23,65,34,82,37,12,17,24,36,82,51

instead of

23,65,5,34,82,37,12,17,24,36,82,51[/B]


What is the correct adjustment to this problem?

#include <iostream>
#include <iomanip>

using namespace std;

void printIt (int numbers[],int length);
int removeAt (int numbers[], int length, int index);
void insertAt (int numbers[], int length, int insertItem, int index);


int main()
{
    
    int numbers[] = {4,23,65,34,82,37,12,17,24,36,82,51};
    int length;
    int index;
    int insertItem;
    
    
    cout<<"Removing an item from the list..."<<endl;
    cout<<endl;
    
    printIt(numbers,12);
    removeAt(numbers,12,index);
    insertAt(numbers,12,insertItem,index);

    
    
    system ("PAUSE");
    return 0;
}

void printIt (int numbers[],int length)
{
     
     cout<<"The current array..."<<endl;
     
    for (int i = 0; i<length; i++)
    {
      cout<<numbers[i]<<" "; 
    } 
     cout<<endl;
     
}

int removeAt (int numbers[], int length, int index)
{
     int item;
  

     cout<<endl;
     cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
     cout<<"Enter the position of the item to be removed."<<endl;
     cout<<"Enter 0 for the first item and so on: ";
     cin>>item;
     
     if (item > length)
     {
     cout<<endl;
     cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
     cout<<endl;
     
     cout<<"The current array..."<<endl;
     
      for (int i = 0; i<length; i++)
    {
      cout<<numbers[i]<<" "; 
    } 
    cout<<endl;
    cout<<endl;
    cout<<"!!!! Index out of Range !!!!"<<endl;
    cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
    cout<<"You entered position "<<item<<", which is OUT OF RANGE."<<endl;
    cout<<"Enter the position of the item to be removed."<<endl;
    cout<<"Enter 0 for the first item and so on: ";
    cin>>item;
    cout<<endl;
    cout<<"After removing the item at position "<<item<<", array is..."<<endl;
    cout<<endl;
    cout<<"The current array..."<<endl; 
    }

    for (int i = 0; i < length; i++)
    {
        if (i != item)
        {
              cout<<numbers[i]<<" ";
        }
    }
    
    cout<<endl;
    cout<<endl;
    cout<<"************************************************************";
    cout<<endl;
    cout<<endl;

     
}


void insertAt (int numbers[], int length, int insertItem, int index)
{
     
     int item;
     
     
     cout<<"Inserting an item in the list..."<<endl;
     cout<<endl;
     cout<<"The current array..."<<endl;
     
     
     for (int i = 0; i < length; i++)
    {
        if (i != item)
        {
              cout<<numbers[i]<<" ";
        }
    }
    cout<<endl;
    cout<<"There are 10 items(s) in the list (position 0 through 11)"<<endl;
    cout<<"Enter item to be inserted and its position"<<endl;
    cout<<"Position of the first element is 0,"<<endl;
    cout<<"so if you want the #5 at the front type in: "<<endl;
    cout<<"5 (space) 0 "<<endl;
    cin>>insertItem;
    cin>>index;
    
    
    if (index > length)
     {
     cout<<endl;
     cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
     cout<<endl;
     
     cout<<"The current array..."<<endl;
     
     for (int i = 0; i < length; i++)
    {
        if (i != item)
        {
              cout<<numbers[i]<<" ";
        }
    }
    cout<<endl;
    cout<<endl;
    cout<<"!!!! Index out of Range !!!!"<<endl;
    cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
    cout<<"You entered position "<<index<<", which is OUT OF RANGE. Please try again."<<endl;
    cout<<endl;
    cout<<"Enter item to be inserted and its position"<<endl;
    cout<<"Position of the first element is 0,"<<endl;
    cout<<"so if you want the #5 at the front type in: "<<endl;
    cout<<"5 (space) 0 "<<endl;
    cin>>insertItem;
    cin>>index;
    }

    cout<<endl;
    cout<<"After inserting the item at position "<<insertItem<<", array is..."<<endl;
    cout<<endl;
    cout<<"The current array..."<<endl;
    
    numbers[insertItem-1] = insertItem;
    cout<<numbers[insertItem-1]<<" ";
    
      for (int i = 0; i < length; i++)
    {
       if (i != item)
        {
        cout<<numbers[i]<<" ";   
        } 
    }
     cout<<endl;
}

Recommended Answers

All 18 Replies

Are you sure your program runs fine? At the first glance, there's so many errors in it:

1. Your main function is passing uninitialized variables' values like index and insertItem into removeAt() and insertAT() functions.

2. Inside insertAt() function, index and insertItem are treated as local variables. That's very bad programming. If you don't intend to pass values, you should simply declare those as local variables instead of parameters.

3. In insertAt() function, the variable item is neither initialized nor assigned any values, yet it is used at the end of the function inside the loop. This leads to some undefined behaviour.

4. You only check once for invalid user input. You should use a loop such that the program will always prompt the user for inputs after each invalid inputs.

5. You use insertItem for the item to be inserted, and index for the position to be inserted. Yet in the below code you never used the variable index! You should used it as the index of the array.

numbers[insertItem-1] = insertItem;    
cout<<numbers[insertItem-1]<<" ";

6. Even if you rectify for point number 5, it still doesn't achieve what you want. It merely overwrite the existing array item with the new item, instead of inserting the item into the position stated. To do what you want, all the subsequent array items must be pushed backwards. If you want to use integer array, then you need to manually "grows" the array to cater for the possible overflow of array.

commented: Good points. +8

Where did you use the variable index to insert the variable insertItem into the array?

Member Avatar for jencas

This is definitely a job for std::vector!

I understand that I need to delete some things and make some changes, but I'm not done with the program yet. My program runs and works. I'm just trying to make this adjustment right now. I will make the further changes after I'm done. Can anyone answer my previous question please?

I understand that I need to delete some things and make some changes, but I'm not done with the program yet. My program runs and works. I'm just trying to make this adjustment right now. I will make the further changes after I'm done. Can anyone answer my previous question please?

Your program most assuredly does NOT run and does NOT work. It runs to completion if you insert a number in such a way so that it doesn't crash and it works under no circumstances as far as I can tell. Reread WaltP's and Denniz's comments. The program does not even do what you state that it does (putting the element in the FRONT of the array). Call your printIt function after you insert/delete and see what I mean, rather than using your cout statements inside of the functions. Also, try inserting a large number (i.e. 100) and see what happens. The cout statements in your insertion function are hindering, not helping, you to debug the program in my view, as they do not reflect what your array contents are after you insert.

Are you sure your program runs fine? At the first glance, there's so many errors in it:

1. Your main function is passing uninitialized variables' values like index and insertItem into removeAt() and insertAT() functions.

2. Inside insertAt() function, index and insertItem are treated as local variables. That's very bad programming. If you don't intend to pass values, you should simply declare those as local variables instead of parameters.

3. In insertAt() function, the variable item is neither initialized nor assigned any values, yet it is used at the end of the function inside the loop. This leads to some undefined behaviour.

4. You only check once for invalid user input. You should use a loop such that the program will always prompt the user for inputs after each invalid inputs.

5. You use insertItem for the item to be inserted, and index for the position to be inserted. Yet in the below code you never used the variable index! You should used it as the index of the array.

numbers[insertItem-1] = insertItem;    
cout<<numbers[insertItem-1]<<" ";

6. Even if you rectify for point number 5, it still doesn't achieve what you want. It merely overwrite the existing array item with the new item, instead of inserting the item into the position stated. To do what you want, all the subsequent array items must be pushed backwards. If you want to use integer array, then you need to manually "grows" the array to cater for the possible overflow of array.

1. What values should I set for index and insertItem? I'm currently using it for user input

2. In the book, the directions stated to set those exact names in the parameters for insertAt, but it didn't tell me to set it to values. I just used it as user input

3. item is used for user input, so i didn't set a value for it

4. How do I make it loop around each time a user inputs a number?

5. For this one, do I need to set Index as a certain value for the piece of code to work? If so, what value am I suppose to set it to?

6. Can you give me an example of how to do this?

Where did you use the variable index to insert the variable insertItem into the array?

I used the index underneath my cout statements for user input. Is it suppose to be placed there?

Your program most assuredly does NOT run and does NOT work. It runs to completion if you insert a number in such a way so that it doesn't crash and it works under no circumstances as far as I can tell. Reread WaltP's and Denniz's comments. The program does not even do what you state that it does (putting the element in the FRONT of the array). Call your printIt function after you insert/delete and see what I mean, rather than using your cout statements inside of the functions. Also, try inserting a large number (i.e. 100) and see what happens. The cout statements in your insertion function are hindering, not helping, you to debug the program in my view, as they do not reflect what your array contents are after you insert.

Well, I was working on this project overnight, so I know I had a lot of mistakes in it. I think the main problem is that I have these variables, but I don't exactly know where they are suppose to go. I'm just following what the book told me to do as far as setting the program up in functions and the parameters etc.

Currently, the program runs, and when I do the removeAt function, it removes the numbers that I want to and the place I want it to be removed. It is just the InsertAt function is giving me trouble when I'm adding numbers. Whatever number I want to add, it just hits the front of the array instead of the position. I know my code for insertAt function is wrong, that is why I need help making the adjustment to this. One other problem I noticed was that I don't know how to keep looping when someone writes a number that is out of range. Can you specifically tell me what "index" is? Is that the same as position? Also, where is insertItem specifically suppose to go?

numbers[insertItem-1] = insertItem;

Does this mean if you insert, oh i dont know, "6432", it goes into numbers[6431]???

If it does I'd look at it again, because I don't think that's what you WANT it to do

Here is your program basically, with more accurate cout statements to reflect the array. What you were displaying before and what was actually in the array are two different things. The display in this revised program is a more accurate display of the array contents. Try inserting a big number like a million into the array and see whether it crashes or runs to completion.

#include <iostream>
#include <iomanip>

using namespace std;

void printIt (int numbers[],int length);
int removeAt (int numbers[], int length, int index);
void insertAt (int numbers[], int length, int insertItem, int index);


int main()
{
    
    int numbers[] = {4,23,65,34,82,37,12,17,24,36,82,51};
    int length;
    int index;
    int insertItem;
    
    
    cout<<"Removing an item from the list..."<<endl;
    cout<<endl;
    
    printIt(numbers,12);
    removeAt(numbers,12,index);
    printIt (numbers, 12);
    insertAt(numbers,12,insertItem,index);
    printIt(numbers,12);
   
    system ("PAUSE");
    return 0;
}

void printIt (int numbers[],int length)
{     
    cout<<"The current array..."<<endl;
     
    for (int i = 0; i<length; i++)
    {
      cout<<numbers[i]<<" "; 
    } 
    cout<<endl;     
}

int removeAt (int numbers[], int length, int index)
{
     int item;
 
     cout<<endl;
     cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
     cout<<"Enter the position of the item to be removed."<<endl;
     cout<<"Enter 0 for the first item and so on: ";
     cin>>item;
     
     if (item > length)
     {
     cout<<endl;
     cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
     cout<<endl;
     
     
     printIt (numbers, length);
    
    
    cout<<endl;
    cout<<endl;
    cout<<"!!!! Index out of Range !!!!"<<endl;
    cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
    cout<<"You entered position "<<item<<", which is OUT OF RANGE."<<endl;
    cout<<"Enter the position of the item to be removed."<<endl;
    cout<<"Enter 0 for the first item and so on: ";
    cin>>item;
    cout<<endl;
    cout<<"After removing the item at position "<<item<<", array is..."<<endl;
    cout<<endl;
    }

    printIt (numbers, length);
}


void insertAt (int numbers[], int length, int insertItem, int index)
{
     
     int item;
     
     
     cout<<"Inserting an item in the list..."<<endl;
     cout<<endl;

     printIt(numbers, length);
     
    cout<<endl;
    cout<<"There are 10 items(s) in the list (position 0 through 11)"<<endl;
    cout<<"Enter item to be inserted and its position"<<endl;
    cout<<"Position of the first element is 0,"<<endl;
    cout<<"so if you want the #5 at the front type in: "<<endl;
    cout<<"5 (space) 0 "<<endl;
    cin>>insertItem;
    cin>>index;
    
    
    if (index > length)
     {
     cout<<endl;
     cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
     cout<<endl;
     
     printIt (numbers, length);
     
    cout<<endl;
    cout<<endl;
    cout<<"!!!! Index out of Range !!!!"<<endl;
    cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
    cout<<"You entered position "<<index<<", which is OUT OF RANGE. Please try again."<<endl;
    cout<<endl;
    cout<<"Enter item to be inserted and its position"<<endl;
    cout<<"Position of the first element is 0,"<<endl;
    cout<<"so if you want the #5 at the front type in: "<<endl;
    cout<<"5 (space) 0 "<<endl;
    cin>>insertItem;
    cin>>index;
    }

    cout<<endl;
    cout<<"After inserting the item at position "<<insertItem<<", array is..."<<endl;
    cout<<endl;
    
    numbers[insertItem-1] = insertItem;
//    cout<<numbers[insertItem-1]<<" ";  // get rid of this line!

    printIt (numbers, length);
}

In some of your displays, you are hardcoding the number of elements in the array into your cout statements (i.e. line 49 and other lines). These should be based on the length variable. Line 93 in particular doesn't make sense. You say you have the array has ten elements, then say those elements are stored in positions 0 through 11.

Alright, I made some corrections with the variables and stuff. It should look atleast alittle better than before, but I'm still have trouble. When I come to my insertAt function and print my current array at the top of the function, it prints out everything except the 8th element in the same order as if I never removed any element. I know something is wrong, but what is the problem? Also, when I insert a number in my insertAt function, the number doesn't get added to the array at all, but it shows that one of my elements is removed. Help!

#include <iostream>
#include <iomanip>

using namespace std;

void printIt (int numbers[],int length);
int removeAt (int numbers[], int length, int index);
void insertAt (int numbers[], int length, int insertItem, int index);


int main()
{
    
    int numbers[] = {4,23,65,34,82,37,12,17,24,36,82,51};
    int length = 12;
    int index;
    int insertItem;
    
    cout<<"Removing an item from the list..."<<endl;
    cout<<endl;
    
    printIt(numbers,12);
    removeAt(numbers,12,index);
    insertAt(numbers,12,insertItem,index);
   // printIt(numbers,12);
   
    system ("PAUSE");
    return 0;
}

void printIt (int numbers[],int length)
{     
     
    for (int i = 0; i<length; i++)
    {
      cout<<numbers[i]<<" "; 
    } 
    cout<<endl;     
}

int removeAt (int numbers[], int length, int index)
{
    
    
     cout<<endl;
     cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
     cout<<"Enter the position of the item to be removed."<<endl;
     cout<<"Enter 0 for the first item and so on: ";
     cin>>index;
     
     while (index > length)
    { 
       if (index > length)
     {
     cout<<endl;
     cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
     cout<<endl;
     
    cout<<"The current array..."<<endl;
    printIt(numbers,12);
    cout<<endl;
    cout<<endl;
    cout<<"!!!! Index out of Range !!!!"<<endl;
    cout<<"There are "<<length<<" item(s) in the list (position 0 through 12)"<<endl;
    cout<<"You entered position "<<index<<", which is OUT OF RANGE."<<endl;
    cout<<"Enter the position of the item to be removed."<<endl;
    cout<<"Enter 0 for the first item and so on: ";
    cin>>index;
    cout<<endl;
    cout<<"After removing the item at position "<<index<<", array is..."<<endl;
    cout<<endl;
    cout<<"The current array is..."<<endl;
    printIt(numbers,12);
    }
     }
     for (int i = 0; i < length; i++)
    {
        if (i != index)
        {
        cout<<numbers[i]<<" ";
        }
    }
    cout<<endl;
    
    
    
    
}


void insertAt (int numbers[], int length, int insertItem, int index)
{

     cout<<endl;
     cout<<"****************************************************"<<endl;
     cout<<endl;
     cout<<"Inserting an item in the list..."<<endl;
     cout<<endl;
     cout<<"The current array..."<<endl;
     for (int i = 0; i < length; i++)
    {
        if (i != index)
        {
              cout<<numbers[i]<<" ";
        }
    }
    cout<<endl;
     
    cout<<endl;
    cout<<"There are 10 items(s) in the list (position 0 through 10)"<<endl;
    cout<<"Enter item to be inserted and its position"<<endl;
    cout<<"Position of the first element is 0,"<<endl;
    cout<<"so if you want the #5 at the front type in: "<<endl;
    cout<<"5 (space) 0 "<<endl;
    cin>>insertItem;
    cin>>index;
    
    
    if (index > length)
     {
     cout<<endl;
     cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
     cout<<endl;
     
     printIt (numbers, length);
     
    cout<<endl;
    cout<<endl;
    cout<<"!!!! Index out of Range !!!!"<<endl;
    cout<<"There are "<<length<<" item(s) in the list (position 0 through 10)"<<endl;
    cout<<"You entered position "<<index<<", which is OUT OF RANGE. Please try again."<<endl;
    cout<<endl;
    cout<<"Enter item to be inserted and its position"<<endl;
    cout<<"Position of the first element is 0,"<<endl;
    cout<<"so if you want the #5 at the front type in: "<<endl;
    cout<<"5 (space) 0 "<<endl;
    cin>>insertItem;
    cin>>index;
    }

    cout<<endl;
    cout<<"After inserting the item at position "<<insertItem<<", array is..."<<endl;
    cout<<endl;
    
    numbers[index-1] = insertItem;
   
 for (int i = 0; i < length; i++)
    {
       if (i != index)
        {
       cout<<numbers[i]<<" ";
        } 
    }

}

any help? i can't figure it out.

When I come to my insertAt function and print my current array at the top of the function, it prints out everything except the 8th element in the same order as if I never removed any element.

int removeAt (int numbers[], int length, int index)
{
       if (index > length)
     {
     cout<<endl;
     cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
     cout<<endl;
     
    cout<<"The current array..."<<endl;
    printIt(numbers,12);
    cout<<endl;
    cout<<endl;
    cout<<"!!!! Index out of Range !!!!"<<endl;
    cout<<"There are "<<length<<" item(s) in the list (position 0 through 12)"<<endl;
    cout<<"You entered position "<<index<<", which is OUT OF RANGE."<<endl;
    cout<<"Enter the position of the item to be removed."<<endl;
    cout<<"Enter 0 for the first item and so on: ";
    cin>>index;
    cout<<endl;
    cout<<"After removing the item at position "<<index<<", array is..."<<endl;
    cout<<endl;
    cout<<"The current array is..."<<endl;
    printIt(numbers,12);
    }
     }
     for (int i = 0; i < length; i++)
    {
        if (i != index)
        {
        cout<<numbers[i]<<" ";
        }
    }
    cout<<endl;
}

It prints out the array as if you never removed an element because you in fact have never removed any element in the removeAt function. Do you see anywhere in the above code where you assign any value to any array element anywhere, a line that looks like this:

numbers[?] = ?;

No? Then the array is unchanged. As I mentioned in my last post, your cout statements are obfuscating your program. That's why I replaced the cout statements in the functions with calls to the printIt function. Your array display code in the functions is not displaying the actual array due to lines like this:

if (i != index)
{
     cout<<numbers[i]<<" ";
}

If you need lines like this to get the array to display correctly, that means that the array itself has not changed correctly. Display the array with the printIt function in order to get a real idea of what the program is doing. To delete an element from an array, you need to move all of the array elements after that element to the left by one:

int indexToDelete = ?;
for (int i = ?; i < ?; i++)
     numbers[i] = numbers[i+1];

You also, when you delete an element, need to adjust the array length since it is now one less than it used to be.

So, I shouldn't use the cin statement for "item"?

So, I shouldn't use the cin statement for "item"?

That's a separate issue. You don't have a variable called item. You have a variable called insertItem. As someone earlier mentioned, you are passing it by value uninitialized to a function, which makes it of no use, plus you are then overwriting it with your cin statement before you use it, which also makes it of no use.

But you have problems before then in your removeAt function, so I'd take care of those first.

Alright, I made the changes for the values to be passed through the functions correctly. I need help with the inserting the number now. I can't seem to get it to work. The code I'm trying to use is at the bottom insertAt function. What is the correct way of doing this?

Here is my updated code:

#include <iostream>
#include <iomanip>

using namespace std;

void printIt (int numbers[],int length);
int removeAt (int numbers[], int length, int index);
void insertAt (int numbers[], int length, int insertItem, int index);


int main()
{
    
    int numbers[12] = {4,23,65,34,82,37,12,17,24,36,82,51};
    int length;
    int index;
    int insertItem;
    
    
    cout<<"Removing an item from the list..."<<endl;
    cout<<endl;
    
    printIt(numbers,12);
    removeAt(numbers,12,index);
    insertAt(numbers,12,insertItem,index);

    
    
    system ("PAUSE");
    return 0;
}

void printIt (int numbers[],int length)
{
     
     cout<<"The current array..."<<endl;
     
    for (int i = 0; i<length; i++)
    {
      cout<<numbers[i]<<" "; 
    } 
     cout<<endl;
     
}

int removeAt (int numbers[], int length, int index)
{

  

     cout<<endl;
     cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
     cout<<"Enter the position of the item to be removed."<<endl;
     cout<<"Enter 0 for the first item and so on: ";
     cin>>index;
     
     if (index > length)
     {
     cout<<endl;
     cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
     cout<<endl;
     
     cout<<"The current array..."<<endl;
     
      for (int i = 0; i<length; i++)
    {
      cout<<numbers[i]<<" "; 
    } 
    cout<<endl;
    cout<<endl;
    cout<<"!!!! Index out of Range !!!!"<<endl;
    cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
    cout<<"You entered position "<<index<<", which is OUT OF RANGE."<<endl;
    cout<<"Enter the position of the item to be removed."<<endl;
    cout<<"Enter 0 for the first item and so on: ";
    cin>>index;
    cout<<endl;
    cout<<"After removing the item at position "<<index<<", array is..."<<endl;
    cout<<endl;
    cout<<"The current array..."<<endl; 
    }

   for (int i = 0; i < 11; i++)
   {
     numbers[i] = numbers[i+1];
     cout<<numbers[i]<<" ";
   }
   
    cout<<endl;
    cout<<endl;
    cout<<"************************************************************";
    cout<<endl;
    cout<<endl;

     
}


void insertAt (int numbers[], int length, int insertItem, int index)
{
     
     
     cout<<"Inserting an item in the list..."<<endl;
     cout<<endl;
     
     printIt(numbers,11);
     
    cout<<endl;
    cout<<"There are 10 items(s) in the list (position 0 through 11)"<<endl;
    cout<<"Enter item to be inserted and its position"<<endl;
    cout<<"Position of the first element is 0,"<<endl;
    cout<<"so if you want the #5 at the front type in: "<<endl;
    cout<<"5 (space) 0 "<<endl;
    cin>>insertItem;
    cin>>index;
    
    
    if (index > length)
     {
     cout<<endl;
     cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
     cout<<endl;

     printIt(numbers,11);
    cout<<endl;
    cout<<endl;
    cout<<"!!!! Index out of Range !!!!"<<endl;
    cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
    cout<<"You entered position "<<index<<", which is OUT OF RANGE. Please try again."<<endl;
    cout<<endl;
    cout<<"Enter item to be inserted and its position"<<endl;
    cout<<"Position of the first element is 0,"<<endl;
    cout<<"so if you want the #5 at the front type in: "<<endl;
    cout<<"5 (space) 0 "<<endl;
    cin>>insertItem;
    cin>>index;
    }

    cout<<endl;
    cout<<"After inserting the item at position "<<insertItem<<", array is..."<<endl;
    cout<<endl;
    cout<<"The current array..."<<endl;
    
    numbers[index-1] = insertItem;
    
    for (int i = 0; i < 11; i++)
   {
     cout<<numbers[i]<<" ";
   }
     cout<<endl;
}

Alright, I will comment base on your latest codes, cos I don't really have time to go through all the posts. Also, I will skip removeAt() function at this moment and focus on the rest.

1. At the main function, you are still passing uninitialized variables "insertItem" and "index" by values to the functions (line 24 and 25). If you have no use for them, take them out from the parameters. Use local variables instead.

2. Line 140 should be this instead:

cout<<"After inserting the item at position "<<index<<", array

3. At line 144, according to your instruction at line 132, it should be the following instead:

numbers[index] = insertItem;

4. Still, if you perform point 3, you are only OVERWRITING the array item, not inserting it. If you are going to continue using integer array for insertion, there are 2 ways you can do about it:

a) Declare a large array size (say 100) initially, fill the first 12 with your numbers and leave the rest. When doing insertion at say position #2, first use a loop to "push back" all items from position #2 onwards, then write the new value at position #2. Something like this:

for(int i=length; i > index;i--)
{
number[i] = number[i-1];
}
number[index] = insertItem;

You need use the variable length to keep track of the up-to-date length of the array that is filled.

b) The second method would be using dynamic memory allocation for the number array. Every time when you want to do insertion, you will have to copy all the content of the number array into a temporary array, then reallocate memory for the number array to expand its size, then copy content of temporary array back into the number array. I don't recommend this method though its more storage efficient, it will introduce you even more errors in the process.

5. As I said in earlier post, checking of invalid inputs can use loop instead. Something like this:

do
{
/// Prompt user for item and index

/// Get user's inputs

/// If user's inputs is invalid, display error message

} while (index > length);

6. A lot of hardcoding here and there which is yet another bad practice, but nevermind get the logic right first.

Thanks for helping me. I'm making this as solved because it works now. I appreciate it. The reason why I have the stuff in the parameters is because the directions told me to put them in the parameters, otherwise, i would use them as local variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.