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!