Xcode vs VS2013 Part2 - Main()

This is a main function written with all codes in 'Xcode vs VS2013 Part1.'
Xcode shows three same error messages.
Line 160 & 171 & 187:
/Users/BB/Desktop/mac1/mac1/main.cpp:490:21: Invalid operands to binary expression ('List<double>::iterator' and 'List<double>::iterator')

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

// FILE: Driver.cpp
#include <iostream>
using namespace std;

template <typename T>
void cinNbr(T&); // enter a number from keyboard

void displayItems(const List<double>&);
void addItems(List<double>&);
void searchItem(const List<double>&);
void removeItem(List<double>&);
void removeCopies(List<double>&);

int main()
{
    List<double> l1;
    int menuChoice = 0;
    bool quit = false;
    while (!quit)
    {
        system("cls"); // clears the console screen
        cout << "1. Show List contents\n";
        cout << "2. Add item(s) to the List\n";
        cout << "3. Search for an item in the List\n";
        cout << "4. Withdraw single item from the List\n";
        cout << "5. Withdraw all copies of an item from the List\n";
        cout << "6. Exit the program\n\n";
        cout << "What would you like to do? Select a number (1 - 6), then press Enter: ";
        cinNbr(menuChoice);
        switch (menuChoice)
        {
            case 1:
                displayItems(l1);
                system("pause"); // causes program to pause until user hits a key
                break;
            case 2:
                cout << endl << "You chose to add value(s) to the List.\n";
                cout << "Type the double(s) that will be stored,\n";
                addItems(l1);
                system("pause"); // causes program to pause until user hits a key
                break;
            case 3:
                cout << endl << "You chose to search for a value in the List.\n";
                cout << "Type the double to be searched:\n";
                searchItem(l1);
                system("pause"); // causes program to pause until user hits a key
                break;
            case 4:
                cout << endl << "You chose to remove a value from the List,\n";
                cout << "Type the double to be removed:\n";
                removeItem(l1);
                system("pause"); // causes program to pause until user hits a key
                break;
            case 5:
                cout << endl << "You chose to remove all copies of a value from the List,\n";
                cout << "Type the double to be completely removed:\n";
                removeCopies(l1);
                system("pause"); // causes program to pause until user hits a key
                break;
            case 6:
                quit = true;
                cout << endl;
                break;
            default:
                cout << endl << "Invalid menu choice. Please try again.\n\n";
                system("pause"); // causes program to pause until user hits a key
        }
    }
    cout << "Closing...\n";
    return 0;
}

template<typename T>
void cinNbr(T& var) // enter a number from keyboard
{
    cin >> var;
    while (cin.fail() || cin.get() != '\n') // if error encountered
    {
        cout << "Invalid entry!  Please re-enter value: ";
        cin.clear(); // clears any error flag
        cin.ignore(80, '\n'); // clears input buffer
        cin >> var;
    }
    cin.clear();
}

void displayItems(const List<double>& doubles)
{
    if (doubles.size() == 0)
        cout << "List empty!\n";
    else if (doubles.size() > 100)
    {
        cout << "List too long to be displayed!\n";
        cout << "List size: " << doubles.size() << endl << endl;
    }
    else
    {
        cout << "Here are the contents:\n";
        for (List<double>::const_iterator iLoc = doubles.begin(); iLoc != doubles.end(); iLoc++)
            cout << (double)*iLoc << ", ";
        cout << "\b\b  \n"; // delete last space and comma
        cout << "List size: " << doubles.size() << endl << endl;
    }
}

void addItems(List<double>& doubles)
{
    cout << "Press \"q\" when you are done:\n";
    double itemInput;
    while (true) // while valid double is entered
    {
        cin >> itemInput;
        if (cin.fail() == false)
            doubles.push_back(itemInput);
        else
        {
            cin.clear(); // clear and ignore bad input
            cin.ignore(80, '\n');
            break; // invalid input
        }
    }
}

void searchItem(const List<double>& doubles)
{
    if (doubles.size() == 0)
        cout << "List empty!\n";
    else
    {
        double itemInput;
        cinNbr(itemInput);
        size_t index = 0;
        List<double>::const_iterator iLoc = doubles.begin();
        while (iLoc != doubles.end())
        {
            if (*iLoc == itemInput)
            {
                cout << "Value " << itemInput << " found at node # " << index << endl;
                break;
            }
            index++;
            iLoc++;
        }
        if (iLoc == doubles.end())
            cout << "Value " << itemInput << " not found!\n";
    }
}

void removeItem(List<double>& doubles)
{
    if (doubles.size() == 0)
        cout << "List empty!\n";
    else
    {
        double itemInput;
        cinNbr(itemInput);
        size_t index = 0;
        List<double>::iterator iLoc = doubles.begin();
        while (iLoc != doubles.end())
        {
            if (*iLoc == itemInput)
            {
                cout << "Value " << itemInput << " removed from node # " << index << endl;
                doubles.erase(iLoc);
                break;
            }
            index++;
            iLoc++;
        }
        if (iLoc == doubles.end())
            cout << "Value not found!\n";
    }
}

void removeCopies(List<double>& doubles)
{
    if (doubles.size() == 0)
        cout << "List empty!\n";
    else
    {
        double itemInput;
        cinNbr(itemInput);
        size_t index = 0;
        size_t nbrDeleted = 0;
        List<double>::iterator iLoc = doubles.begin();
        while (iLoc != doubles.end())
        {
            if (*iLoc == itemInput)
            {
                cout << "Value " << *iLoc << " removed from node # " << index << endl;
                iLoc = doubles.erase(iLoc);
                nbrDeleted++;
            }
            else
                iLoc++;
            index++;
        }
        if (nbrDeleted == 0)
            cout << "Value not found!\n";
    }
}

Last edited on
Don't start a new thread for every single question about a general topic.

Did you define an overload for operator!=() that takes two List<double>::iterators?
Last edited on
Hi Helios,

I am sorry. I am new to this forum, so I didn't mean to do that.
My programming experience is so short (2 months) that I am not yet familiar with some concepts. Can you please explain why one should define an overload operator!=() ?

x != y is equivalent to a function call of the form operator!=(x, y), but this doesn't compile if an overload for operator!=() that takes parameters of the corresponding types, isn't defined. The language doesn't automatically generate relational operators for user-defined types.
Hi helios,

Thank you for your reply.
Can you shoe me an example of an overload for operator!=()?
For example, something trivial but not very useful:
1
2
3
bool operator!=(const std::string &a, const std::string &b){
    return a.size() != b.size();
}
Topic archived. No new replies allowed.