I am having a problem writting a function to remove all occurences of a specific (user defined) integer from an array.

Here is what I have so far.

#include<iostream>
using namespace std;

void menuDisplay(int& menuSelect);
void remove(int intArray[], int& sizeOfArray, int removeItem);
void removeAt(int intArray[], int& sizeOfArray, int index);
void removeAll(int intArray[], int& sizeOfArray, int removeAll);
void printArray(int intArray[], int sizeOfArray);
void insertAt(int intArray[], int& sizeOfArray, int insertItem, int index);
int main(){

    int sizeOfArray = 10;
    int index;
    int intArray[10];
    int menuSelect = 0;
    cout << "Please enter numbers for array" << endl;
    
    for (int f = 0; f < sizeOfArray; f++)
        cin >> intArray[f];
    
    cout << endl;
    cout << "The array you entered is: " << endl;
    cout << "**********************" << endl;

    for (int f = 0; f < sizeOfArray; f++)
        cout << intArray[f] << " ";
    cout << endl;
    cout << "**********************" << endl;
    
    while (menuSelect != 6)
    {
       menuDisplay(menuSelect);
        switch (menuSelect){
            case 1:
                int removeItem;
                cout << "Enter item which you want to remove."<<endl;
                cin >> removeItem;
                remove(intArray, sizeOfArray, removeItem);
                break;
            case 2:
                cout << "Please enter index" << endl;
                cin >> index;
                removeAt(intArray, sizeOfArray, index);
                break;
            case 3:
                int removeAll;
                cout << "Please enter number to remove multiple occurences." << endl;
                cin >> removeAll;
                removeAll(intArray, sizeOfArray, removeAll);
                break;
            case 4:
                int insertItem;
                cout << "Please enter insertItem and index." << endl;
                cin >> insertItem >> index;
                insertAt(intArray, sizeOfArray,insertItem,index);
                break;
            case 5:
                printArray(intArray, sizeOfArray);
                break;
            default:
                cout << "invalid selection" << endl;
        }
    }
    return 0;
}
void menuDisplay(int& menuSelect)
{
    cout << endl;
    cout << "[[Menu]]" << endl;
    cout << "1. removeItem" << endl;
    cout << "2. removeAt" << endl;
    cout << "3. removeAll" << endl;
    cout << "4. insertAt" << endl;
    cout << "5. print array" << endl;
    cout << "Please make enter a number to continue." << endl;
    cout << endl;
    cin >> menuSelect;
}

void remove(int intArray[], int& sizeOfArray, int removeItem)
{
        int num = 999;
        int i;
        for (int i = 0; i < sizeOfArray; i++)
        if (intArray[i] == removeItem)
            num=i;
        if (num!=999)
        {
            for (int k = num; k < sizeOfArray; k++)
            intArray[k] = intArray[k+1];
            sizeOfArray--;
        }    
        if(num==999)
        cout << "Error" << endl;
        
}


void removeAt(int intArray[], int& sizeOfArray, int index)
{

        if (index >= 0 && index < sizeOfArray)
        {
            for (int k = index; k < sizeOfArray; k++)
            intArray[k] = intArray[k+1];
            sizeOfArray--;
        }
        else
            cout << "Error: the index you entered is out of range." << endl;


}

void removeAll(int intArray[], int &sizeOfArray, int removeAll)
{
    int counter; 
    int num = 999;
    int i, k;

       for (int i = 0; i < sizeOfArray; i++)
        if (intArray[i] == removeAll)
            num = i;
        if (num!=999)
        {
            for (int k = num; k < sizeOfArray; k++)
            intArray[k] = intArray[k+1];
            sizeOfArray--;
        }    
        if(num==999)
        cout << "Error" << endl;
}

void insertAt(int intArray[], int& sizeOfArray, int insertItem, int index)
{
    if (index >= 0 && index < sizeOfArray)
    {
        for (int i = sizeOfArray; i > index; i--)
        {
            intArray[i] = intArray[i-1];            
        }
        sizeOfArray++;
        intArray[index]= insertItem;
    }
}
void printArray(int intArray[], int sizeOfArray)
{
        cout << "**********************" << endl;
        for (int f = 0; f < sizeOfArray; f++)
            cout << intArray[f] << " ";
        cout << endl;
        cout << "**********************" << endl;
}

Line (51) : error C2064: term does not evaluate to a function taking 3 arguments

Recommended Answers

All 4 Replies

The error is caused by your naming a variable the same as the function

case 3:
     int removeAll;  //this variable masks the function call that follows
     cout << "Please enter number to remove multiple occurences." << endl;
     cin >> removeAll;
     removeAll(intArray, sizeOfArray, removeAll);
     break;

Looking at the removeAll( ) function, step through it by hand. What happens if the target value exists more than once? It looks like only the last occurrence will be removed.

Build upon what you've already done. You have a method that can remove a value at a specific index. Why not have the removeAll( ) function call that, every time it finds a matching value? Don't rewrite when you can reuse.

Is it really needed to output an error message if no matching values are found?

I've reworked the removeAll() to this:

void removeAll(int intArray[], int& sizeOfArray, int removeAll)
{
    int counter=0;
	int lower=0;
	int loc=999;
	int i, k;
    do
	{
		for(int j = 0; j < sizeOfArray; j++)
			if(intArray[i]== removeAll)
			{
				for(int k = j; k < sizeOfArray; k++)
					intArray[k] = intArray[k+1];
					lower++;
					loc++;
			}						
		counter++;
	}
	while(counter < sizeOfArray);
	if(loc == 999||sizeOfArray <= 0)
		cout << endl <<"Error: integer could not be found."<<endl;
		sizeOfArray = sizeOfArray-lower;

but I am still getting that same error even if I remove the

int removeAll;

The error output is just for testing purposes.

I got it to work. Thanks for the advice vmanes. I saw my error after re-reading your post.

Glad you got it.

Two suggestions for your future posting.
When posting long code and referencing line numbers, as the error message did, use code tags to enclose the code. This will give line numbers like you see in my reply above.

Also, work on your indenting. You have a couple possibly confusing spots, where it's not clear your intent. For example

if (num!=999)
        {
            for (int k = num; k < sizeOfArray; k++)
            intArray[k] = intArray[k+1];
            sizeOfArray--;
        }    

what is it that you want to happen each iteration of the for loop? This would be better as:

if (num!=999)
        {
            for (int k = num; k < sizeOfArray; k++)
                  intArray[k] = intArray[k+1];
            sizeOfArray--;
        }       

Now it's pretty clear to the reader that only the array assignment is executed, the decrement of size is a separate action after the loop completes. Even clearer as:

if (num!=999)
        {
            for (int k = num; k < sizeOfArray; k++)
           {
                  intArray[k] = intArray[k+1];
           }

            sizeOfArray--;
        }             

Don't be afraid to use a little extra vertical whitespace to separate code chunks.

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.