Duplicating output

The problem is in the function remove(), it's causing 'guave' to duplicate and also is removing strawberry from the output in the 'remove banana' and 'remove orange'. How could I fix this?

Also, I'm having a problem about setting off a error if removeItem isn't found in the array. I tried different ways but it keeps looping giving off the error in till the word is found in the array.

Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
insert pear
len=7 apple,  bannana,  guava,  mango,  orange,  pear,  strawberry,
******************************
insert avacado
len=8 apple,  avacado,  bannana,  guava,  mango,  orange,  pear,  strawberry,
******************************
insert figs
insert maximum exceeded adding value: figs
len=8 apple,  avacado,  bannana,  guava,  mango,  orange,  pear,  strawberry,
******************************
remove bannana
len=7 apple,  avacado,  guava,  guava,  mango,  orange,  pear,
******************************
remove orange
len=6 apple,  avacado,  guava,  guava,  mango,  pear,
******************************


Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

const int MAX_ARRAY_SIZE=8;

bool insert(string arr[], int & len, string value)
{
    if (len >= MAX_ARRAY_SIZE){
        cout << "insert maximum exceeded adding value: " << value << endl;
        return false;
    } else {
        for (int findWord = len; findWord >= len - 1; findWord--){
            arr[findWord] = value;
            len += 1;
        }
        return true;
    }
}
bool remove(string arr[], int & len, string removeItem)
{
    for (int i = 0; i < len; i++)
    {
        if (removeItem == arr[i]){
            arr[i] = arr[i+1];
            len -= 1;
            return true;
            }
    }
}
void printArr(string arr[], int len)
{
    cout << "len=" << len;

    for (int print = 0; print < len; print++) {
        sort(arr + 1, arr + len);
        cout << " "  << arr[print] << ", ";
    }

    cout << endl << "******************************" << endl;
}

int main()
{
    string strArr[MAX_ARRAY_SIZE] ={"apple", "bannana", "guava", "mango",  "orange", "strawberry"};
    int len = 6; // Number of current items

    cout << "insert pear"<< endl;
    insert(strArr,  len, "pear");
    printArr(strArr, len);

    cout << "insert avacado"<< endl;
    insert(strArr,  len, "avacado");
    printArr(strArr, len);

    cout << "insert figs"<< endl;
    insert(strArr,  len, "figs");
    printArr(strArr, len);

    cout << "remove bannana"<< endl;
    remove(strArr,  len, "bannana");
    printArr(strArr, len);

    cout << "remove orange"<< endl;
    remove(strArr,  len, "orange");
    printArr(strArr, len);

    return 0;
}
First, you shouldn't be using "==" with strings. You can use stringName.compare(string2Name) to compare two strings.

EDIT:
You can use "==" with strings. Apparently, I have been instructed improperly for all of my education. Apologies for the incorrect information.
Last edited on
It is because in the remove function, you don't remove the object in the array, you just set it to the next object in the array.
How would I go about removing it then from the array?
I would suggest using a vector instead of an array, cause it is much easier to add to and delete things from a vector.
you shouldn't be using "==" with strings
with two std::string's this should not be a problem.

Performs the appropriate comparison operation between the string objects lhs and rhs.

The functions use string::compare for the comparison.

http://www.cplusplus.com/reference/string/string/operators/
Last edited on
Topic archived. No new replies allowed.