Okay, I have another problem. It has nothing to do with the file I/O streams. But I want to make modifications with this code. It works but I would like to make changes though. PLEASE NOTHING TOO FANCY, I'm still a beginner. Thank you for the help!

       Write a function, removeAt, that takes three parameters: an array of
       integers, the number of elements in the array, and an integer (say, index). The
       function should delete the array element indicated by index. If index is out of
       range or the array is empty, output an appropriate message. (Note that after
       deleting the element, the number of elements in the array is reduced by 1.) Assume
       that the array is unsorted.


#include <iostream>//include statement(s)
#include <iomanip>

using namespace std;//using namespace statement(s)

const int MAX_SIZE = 100;//constant variable(s)

void showIndexInfo(int arr[], int &elements);//void function header(s)
void showArray(int arr[], int &elements);
void removeAt(int arr[], int &elements, int index);

int main()
{
    int numElem = 5;//variable declaration(s)
    int index = 0;
    int intArray[100] = {1, 2, 3, 4, 5};
    char redo;

    cout << "The current array is: ";
    showArray(intArray, numElem);//void function call(s)

    do
    {
        removeAt(intArray, numElem, index);
        showArray(intArray, numElem);

        cout << "Would you like to remove another number from the new array?" << endl
             << "(Y/y for yes - other to close): ";
        cin >> redo;
    } while ((redo == 'y') || (redo == 'Y'));//do while loop for menu

    cout << endl << "Program is exiting...";
    cout << endl;

    system ("PAUSE");//black box to appear
    return 0;//return statement(s)
}

void showArray(int arr[], int &elements)//void function to show the array
{
    for (int i = 0; i <= (elements - 1); i++)//for loop prints the numbers from each element
    {
        cout << arr[i] << " ";
    }

    cout << endl << endl;
}

void removeAt(int arr[], int &elements, int index)//void function to remove element of array
{
    cout << endl
         <<"Please select the index of the number you want to remove from the array: ";
    cin >> index;//user inputs index to delete

    if (index < 0 || index > elements - 1)//if statement for index is less than 0 or greater than the elements
    {
        cout << "The value for the index you have entered is invalid." << endl
             << "The array is still: ";
    }

    else
    {
        for (int i = index + 1; i <= (elements - 1); i++)//for loop to remove the index with number in element
        {
            arr[i - 1] = arr[i];
        }

        cout << "The new array is now: ";
        elements--;
    }   
}

Thank you for helping me out!

First off the index to remove at should be passed into the function. If you need that input from the user, do it before you call the function.

the loop limit can just be < elements. Doing a math operation(element - 1) on every iteration like that, when it's not necessary, is inefficient.

Can you show me how to do this though? Sorry I'm more of a visual learner.

void removeAt(int arr[], int &elements, int index)//void function to remove element of array
{
    if (index < 0 || index >= elements)//if statement for index is less than 0 or greater than the elements
    {
        cout << "The value for the index you have entered is invalid." << endl
            << "The array is still: ";
    }
    else
    {
        for (int i = index + 1; i < elements; i++)//for loop to remove the index with number in element
        {
            arr[i - 1] = arr[i];
        }
        cout << "The new array is now: ";
        elements--;
    }
}

Oh! I see what you're saying. Okay, I changed the things but...for some reason, it deletes 1 and it continues off with asing "would you like to repeat this?" How can I fix this?

Your code is set up to ask for Y or y to remove another element or any other character to quit. If that is what it is doing you'll have to change that.

instructor asked for a repeat though. I'm not sure how to make a 'menu' function, but I can make a menu function though - its allowed

The steps are fairly simple, printout the menu,get the input from the user, use a switch block to run whichever function is indicated. Encase it in a while loop that runs until the quit character is entered.

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.