Removing any element on a list

I want to add a remove feature by accessing any part of the list.
for example:
I added 3 names
Mark
Max
May

I want to remove it by searching the name itself and it will remove a part of
the list that matches with the name.

example:
Enter the name you want to remove: Max

So when i view the names it will show.
Mark
May

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
#include <iostream>
#include <string>
#include <list>
using namespace std;


int main ()
{
	
	list <string> names;
	string name;
	string remove;
	int option;
	
	list <string> :: iterator it = names.begin();
	list <string> :: iterator show;
	
	main:
	system("cls");
	cout << "[1] Enter name: \n";
	cout << "[2] View names: \n";
	cout << "[3] Remove name: \n";
	cout << "\nInput: ";
	cin >> option;
	
	switch (option)
	{
		case 1:
			system("cls");
			cout << "Enter your name: ";
			cin >> name;
			names.insert(it, name);
			cout << "\n\nInput any number to go back: ";
			cin >> option;
			if (option >= 0) {
				goto main;
			} else {
				goto main;
			}
		break;
		
		case 2:
			system("cls");
			cout << "List of Names: \n\n";
			for (show = names.begin(); show != names.end(); show++) {
				cout << *show << endl;
			}
			cout << "\nEnter any number to go back: ";
			cin >> option;
			if (option >= 0) {
				goto main;
			} else {
				cout << "Invalid input";
			}
		break;
		
		case 3:
			system("cls");
			cout << "Enter the name you want to remove: ";
			cin >> remove;
			if (remove == names.begin()) {
				
			}
			
	}	
	
}
names.remove(name_to_remove);
std::list can remove an element by value:

http://www.cplusplus.com/reference/list/list/remove/

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
#include <iostream>
#include <list>
#include <string>

int main()
{
   // populate a list with some strings
   std::list<std::string> my_list { "Able", "Baker", "Charlie", "Max", "Uniform", "Zed" };

   // let's show the list
   std::cout << "The original list:\n";
   for (const auto& itr : my_list)
   {
      std::cout << itr << ' ';
   }
   std::cout << "\n\n";

   std::cout << "Removing \"Max\".....\n\n";

   my_list.remove("Max");

   std::cout << "The new list:\n";
   for (const auto& itr : my_list)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';
}

The original list:
Able Baker Charlie Max Uniform Zed

Removing "Max".....

The new list:
Able Baker Charlie Uniform Zed

If you don't want to "hard-code" the value, replace lines 18-20 with
18
19
20
21
22
   std::string str { "Max" };

   std::cout << "Removing \"" << str << "\".....\n\n";

   my_list.remove(str);
@Furry Guy your example will only work if there is initial value right ? what if theres no initial value ?

I want it to be like this.

[0]Exit
[1]Add name
[2]View name
[3]Remove name
Input: 1
------------------
Enter name: Mark
Enter any number to go back: 1
------------------
[0]Exit
[1]Add name
[2]View name
[3]Remove name
Input: 1
------------------
Enter name: Max
Enter any number to go back: 1
------------------
[0]Exit
[1]Add name
[2]View name
[3]Remove name
Input: 1
------------------
Enter name: May
Enter any number to go back: 1
------------------
[0]Exit
[1]Add name
[2]View name
[3]Remove name
Input: 2
------------------
Mark
Max
May

Enter any number to go back: 1
------------------
[0]Exit
[1]Add name
[2]View name
[3]Remove name
Input: 3
------------------
Enter the name you want to remove: Max
Enter any number to go back: 1
------------------
[0]Exit
[1]Add name
[2]View name
[3]Remove name
Input: 2
------------------
Mark
May

Enter any number to go back:
------------------
Last edited on
No, std::list::remove will work no matter how you fill the list. Your menu system if properly coded will handle removing a specific element just fine.

I showed how to use the function, now it for you to use it in your current code.
Do i need another container for the elements so i can make a condition to find the element i want to remove ?
No.
Can you give me an easier explanation on how i can implement it on my code, please.
To remove the name "Mark" from the list names, say
names.remove("Mark");
What's confusing you?
Last edited on
Lines 61 - 63, change to this:

names.remove(remove);
A lot of what you have is good. It can be simplified though.
- goto's are not a good idea - gets too complicated etc
- you'll need another option in the menu to get out of the while loop
- give the variables self explanatory names eg 'remove_this_name' doesn't leave much doubt.
- I scrapped the cls lines for clarity

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
#include <iostream>
#include <string>
#include <list>

using namespace std;

int main ()
{
    list <string> names;
    string name;
    string remove_this_name;

    string menu{ "[1] Enter name: \n[2] View names: \n[3] Remove name: \n\n"};

    int option = -1;

    list <string> :: iterator it = names.begin();
    list <string> :: iterator show;

    cout << menu;

    while( option < 0 || option > 3 )
    {
        cout << "Input: ";
        cin >> option;

        switch (option)
        {
        case 1:
            cout << "Enter your name: ";
            cin >> name;
            names.insert(it, name);
            break;

        case 2:
            cout << "List of Names: \n\n";
            for (show = names.begin(); show != names.end(); show++)
            {
                cout << *show << endl;
            }
            break;

        case 3:
            cout << "Enter the name you want to remove: ";
            cin >> remove_this_name;
            names.remove(remove_this_name);
            break;

        default:
            cout << "*** INVALID OPTION ***\n";
            break;
        }

        cout << menu;
        option = -1;
    }

    return 0;
}
PS Instead of the iterator being 'show' maybe 'name_in_list' might add clarity.
Topic archived. No new replies allowed.