I figured as much. I'll just paste the whole code since it's relatively small. This is an assignment for a course, which should be obvious...
This program reads from a file with multiple lines of single integers and puts them into an array. The array is then displayed and the program prompts to "delete" an occurrence of a certain value. After deletion, every element to the right of that value is shifted one to the left, and however many variables that were deleted are treated as vacant elements at the "end" of the array.
I can add a variable to other functions, just not under main()..
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void fillDisplayArray(int fillit[], int& howMany);
void removeAll(int arrayData[], int numInArray, int removeIt, int& numRemoved);
int main()
{
int array[20], i = 0, numReturned = 0;
int killIt = 0, numKilled = 0;
fillDisplayArray(array, numReturned);
cout << "\nThere are a total of " << numReturned << " elements in the file\nwhich were loaded into an array."
<< "\n\nWhich occurrence of an integer would you\nlike to remove from the array?: ";
cin >> killIt;
removeAll(array, numReturned, killIt, numKilled);
if (numKilled == 0)
cout << "\nSorry, there are no occurences of " << killIt << " in the array.\n";
else if (numKilled != 0)
cout << endl << numKilled << " occurence(s) of " << killIt << " were removed from the array\n"
<< "There are now " << numReturned - numKilled << " elements in the array: ";
cout << "\nThe array now consists of: \n\n";
for (i = 0; i <= numReturned - 1 - numKilled; i++)
{
cout << setw(10) << left << array[i];
if (i == 4 || i == 9 || i == 14)
cout << "\n";
}
return 0;
}
void fillDisplayArray(int arrayData[], int& numReturned)
{
int i, c = 0;
ifstream inFile;
inFile.open("Ch10_Ex5-7Data.txt");
cout << "The following numbers were read from the input file:\n\n"; <--- THIS AREA displays after adding a variable to the code, and then a segfault occurs, when filling the array, it seems
inFile >> arrayData[i];
while (!inFile.eof())
{
cout << setw(10) << left << arrayData[i] ; c++;
i++;
inFile >> arrayData[i];
if (c == 5)
{
cout << "\n";
c = 0;
}
}
numReturned = i;
inFile.close();
}
void removeAll(int arrayData[], int numInArray, int removeIt, int& numRemoved)
{
int i, location;
for (i = 0; i <= numInArray; i++)
{
if (arrayData[i] == removeIt)
{
for (location = i; location < numInArray - 1 ; location++)
{
arrayData[location] = arrayData[location + 1];
}
numRemoved = numRemoved + 1;
}
}
}