Need help with the menu loop and a function

I'm writing a phone book application, and I am having trouble looping the main menu here. When I go to select 1,2,3,4 it is just looping the initial cout statements. Also, I need help with the remove contacts function, having trouble with it. Thanks!

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


using namespace std;
vector<string>Names;
vector<string>Phones;
vector<string>Adresses;

void addContact();
void searchByID();
void searchByName();
void removeContact();

int main()
{

	int sel = -1;
	while (sel)
	{
		cout << "Chose a Number to Execute the Selected Option: " << endl;
		cout << "[1]New Contact" << endl;
		cout << "[2]Search By ID" << endl;
		cout << "[3]Search By Name" << endl;
		cout << "[4]Remove Contact" << endl;
		cout << "[5] Exit PhoneBook" << endl;
		cout << "Your choice: " << endl;
		cin >> sel;
	}
	
	switch (sel)
	{

	case 1:
		addContact();
		break;
	case 2:

		searchByID();
		break;
	case 3:
		searchByName();
		break;
	case 4:
		removeContact();
		break;
	default:
		cout << "Does not recognice Selection";
		break;
	}
	if (sel == 5)
	{
		return 0;
	}


	system("pause");
	return 0;
}

void addContact()
{
	string name;
	string phone;
	string address;
	cout << "Enter Contact Name: ";
	cin >> name;
	cout << "Enter Contact Phone Number: ";
	cin >> phone;
	cout << "Enter the Address: ";
	cin >> address;
	cout << "The ID for this contact will be\n" << name << endl << phone;
	Names.push_back(name);
	Phones.push_back(phone);
	Adresses.push_back(address);
}
void searchByID()
{

	int value;
	cout << "Enter your Contact’s ID that you need to search: ";
	cin >> value;
	if (value > Names.size())
	{

		cout << "This ID does not exist";
		return; //terminates it
	}
	cout << "--Here is a little information on the contact ID--";
	cout << "Name: " << Names[value] << endl; //index is this and this is used to differentiate the values within the vector
	cout << "Phone Number: " << Phones[value] << endl;
	cout << "Address: " << Adresses[value] << endl;
}
void removeContact()
{

}
void searchByName()
{

	bool found = false; //bool is a data type that is only true or false, so this will tell us if the search was either successful or failed
	string name;
	cout << "Enter the Contact Name to search: ";
	cin >> name;
	for (int i = 0; i != Names.size(); i++)
	{
		if (Names[i] == name)
		{

			cout << "Name:" << Names[i] << endl;
			cout << "Phone:" << Phones[i] << endl;
			found = true;

		}

	}
	if (!found)
	{
		cout << "Contact was not found" << endl;
	}
}
It's looping cout statements because it will always evaluate true. I have done
countless menu-based programs during my freshie years, and the best way (IMHO)
is to do them like this:

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
int main()
{
  bool inMenu = true;
  int sel;
  while (inMenu) {
    cout << "Chose a Number to Execute the Selected Option: " << endl;
    cout << "[1]New Contact" << endl;
    cout << "[2]Search By ID" << endl;
    cout << "[3]Search By Name" << endl;
    cout << "[4]Remove Contact" << endl;
    cout << "[5] Exit PhoneBook" << endl;
    cout << "Your choice: " << endl;
    cin >> sel;

    switch (sel)
    {

      case 1:
        addContact();
        break;
      case 2:

        searchByID();
        break;
      case 3:
        searchByName();
        break;
      case 4:
        removeContact();
        break;
      case 5:
        inMenu = false;
        break;
      default:
        cout << "Does not recognice Selection";
        break;
    } // end of switch

  } // end of menu

  return 0;

} // end of main 


If you want to remove a person, you can do by using the erase function that the vector provides. Your vectors behave like parallel arrays. If you want to remove the third element of the vector, you would do myVector.erase(myVector.begin()+2)
thanks, easy fix went brain dead for a second
Now when I go to add the contact it errors up, has something to do with these lines I believe, does anyone have any idea? It lets me go until I enter the address.

cout << "The ID for this contact will be\n" << name << endl << phone;
Names.push_back(name);
Phones.push_back(phone);
Addresses.push_back(address);
Last edited on
Topic archived. No new replies allowed.