address book using lits

Hi! please help me with my home work.

This assignment is to create a contact book so you can keep track of your friends. With this contact book you will both be able to add friends and view which friends that you have added.

Start by creating an empty list of strings called list_of_friends and make that variable global
Create a function called add_friend which will ask the user for the friend's name and append it to the list of friends
Create a second function called view_friends which will print out all the friends in the list using a range based loop
Create a third function called print_menu that prints the three options:
1: Add

2: View

3: Exit

The menu,controlled from the main function shall get user input to the variable choice to run the menu
The menu shall use a do-while loop until the user selects 3, then it should break the loop and the program will end
The print_menu shall appear after all input (placed in the beginning of the do-while loop)
If the user selects 1, it should call the add_friend-function
Else if the user selects 2, it should call the view_friends-function
No need to have check for invalid input

this is where I am so far!
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
#include <iostream>
#include <list>
using namespace std;

    list <string> list_of_friends;
    void add_friend()
    {
        string name;
        cout<<"enter a name";
        cin>>name;
        list_of_friends.push_front(name);
    }
    void view_friends()
    {
        int i;
        for(i=0; i<3; i++)
        {
            cout<<i;
        }
    }
    void print_menu()
    {
         char ch=0;
         string menu_string= "--- Select ---\n"
                            "1:add\n"
                            "2:View\n"
                            "3: Exit\n";
        do
        {
            cout<<menu_string;
        }
        while((ch=cin.get()) !='3');
        do
        {
            add_friend;
        }
        while((ch=cin.get()) =='1');
        do
        {
            view_friends;
        }
        while((ch=cin.get()) =='2');

    }

int main()
{
    print_menu();
    return 0;
}
And the C++ question is?

You've told us what your assignment is, you've posted code you think fulfills the requirement.
my C++ question is "How to make this code works" :)
I know it's not working and I need a help from someone will correct the code and I need to understand it :)
Perhaps:

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

std::list<std::string> list_of_friends;

void add_friend() {
	std::string name;

	std::cout << "Enter a name: ";
	std::getline(std::cin, name);
	list_of_friends.push_front(name);
}

void view_friends() {
	for (const auto& f : list_of_friends)
		std::cout << f << '\n';
}

unsigned print_menu() {
	unsigned opt {};

	std::cout << "\n--- Select ---\n" <<
		"1: Add\n" <<
		"2: View\n" <<
		"3: Exit\n";

	std::cout << "Enter option: ";
	std::cin >> opt;
	std::cin.clear();
	std::cin.ignore();

	return opt;
}

int main() {
	unsigned opt {};

	do {
		switch (opt = print_menu()) {
			case 1:
				add_friend();
				break;

			case 2:
				view_friends();
				break;

			case 3:
				break;

			default:
				std::cout << "Invalid option\n";
				break;
		}
	} while (opt != 3);
}

Hi, thank you but to make it more complicated I would like to use the functions I learned so far on the course. I do not think my teacher will accepted this :) I did some modifications but I do not understand why the compiler is printing the menu several times and the "view" is not working, see the code below:

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

    list <string> list_of_friends;
    void add_friend()
    {
        string name;
        cout<<"Enter name: ";
        getline(cin, name);
        list_of_friends.push_front(name);
    }
    void view_friends()
    {
           for(string name: list_of_friends)
       {
           cout << name << endl;
       }
    }
    void print_menu()
    {
         char ch=0;
         string menu_string= "--- Select ---\n"
                            "1:add\n"
                            "2:View\n"
                            "3: Exit\n";
        do
       {
           cout<<menu_string;
           if (ch == '1') add_friend();
           else if (ch == '2') view_friends();
       }
       while((ch=cin.get()) !='3');
    }

int main()
{
    print_menu();
    return 0;
}
Follow through the logic of your do loop. Print the menu. Check value of ch and perform action as appropriate. Get value of ch. If not exit then repeat loop and print the menu...

In my version the input was part of the menu display. In that above this is split into 2 separated parts. Displaying the menu and obtaining the input is really one action.
You COULD technically use goto, I know, I know, please don't scream at me for using goto, but hey, I'm just proposing an alternative solution. Besides, it works, so I'm not complaining.

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

    list <string> list_of_friends;
    void add_friend()
    {
        string name;
        cout<<"Enter name: ";
        cin >> name;
        list_of_friends.push_front(name);
    }
    void view_friends()
    {
        for(string name: list_of_friends)
        {
            cout << name << endl;
        }
    }
    void print_menu()
    {
        myLabel:
        char ch;
        string menu_string= "--- Select ---\n"
                            "1:add\n"
                            "2:View\n"
                            "3: Exit\n";
        cout << menu_string;
        cin >> ch;
        if (ch == '1')
        {
            add_friend();
            goto myLabel;
        }
        else if (ch == '2')
        {
            view_friends();
            cout << "\n";
            goto myLabel;
        }
        else if (ch == '3')
        {
            exit(0);
        }
        else
        {
            cout << "\nInvalid option.";
            goto myLabel;
        }
    }
    

int main()
{
    print_menu();
    return 0;
}
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
#include <iostream>
#include <list>
using namespace std;

list <string> list_of_friends;

void add_friend()
{
    string name;
    cout<<"Enter name: ";
    getline(cin, name);
    list_of_friends.push_front(name);
}

void view_friends()
{
    for(string name: list_of_friends)
    {
        cout << name << endl;
    }
}

void print_menu()
{
    char ch = 0;
    string menu_string = "--- Select ---\n"
    "1:add\n"
    "2:View\n"
    "3: Exit\n";
    
    while( cout << menu_string && cin  >> ch && ch != '3')
    {
        cin.clear();
        cin.ignore(1000,'\n');
        
        if (ch == '1') add_friend();
        if (ch == '2') view_friends();
    }
}

int main()
{
    print_menu();
    return 0;
}



--- Select ---
1:add
2:View
3: Exit
1
Enter name: Betty Bloggs
--- Select ---
1:add
2:View
3: Exit
2
Betty Bloggs
--- Select ---
1:add
2:View
3: Exit
1
Enter name: Bill Smith
--- Select ---
1:add
2:View
3: Exit
2
Bill Smith
Betty Bloggs
--- Select ---
1:add
2:View
3: Exit
3
Program ended with exit code: 0
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
#include <iostream>
#include <list>
#include <string>

using namespace std;

std::list<std::string> list_of_friends;

void add_friend() { 
    std::string name;
    cout << "Enter friend's name: ";
    getline(cin, name);
    list_of_friends.emplace_back(name);
    cout << "Friend added." << endl;
    cout << endl;
}

void view_friends() { // if the list is empty show message, else loop and print the names
    if (list_of_friends.size() == 0) {
        cout << "You have no friends." << endl;
    }
    else {
        for (auto& i : list_of_friends) {
            cout << i << endl;
        }
    }
    cout << endl;
}

void print_menu() {
    cout << "1. Add" << endl;
    cout << "2. View" << endl;
    cout << "3. Exit" << endl;
}

int main() {
    int input;
    do{
     print_menu();
     cout << "Enter input: ";
     cin >> input;
     cin.clear(); // this clears the cin so you can enter a name later
     cin.ignore(INT_MAX, '\n');
     if (input == 1) {
         add_friend();
     }
     if (input == 2) {
         view_friends();
     }
    } while (input != 3);

    return 0;
}
You COULD technically use goto, I know, I know, please don't scream at me for using goto,


No, I'll just howl......
Topic archived. No new replies allowed.