Break out from while-loop with switch

I want to break out from the shile-loop from a switch case. DonĀ“t know how Anybody know how to?

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
  // Phonebook

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

class Phonebook
{
public:
	Phonebook() : name(""), number(0) {}
	Phonebook(const string &_name, int _number) : name(_name), number(_number) {}
	Phonebook(const Phonebook &rhs) : name(rhs.name), number(rhs.number) {}
	Phonebook operator=(const Phonebook &rhs)
	{
		if (&rhs == this)
		{
			return *this;
		}
		
		name = rhs.name;
		number = rhs.number;
		return *this;
		
	}
	
	//Metod: Sets the necessary info
	void SetInfo(const string &_name, int _number)
	{
		name = _name;
		number = _number;
	}
	
	const string &Name() const { return name; }
	int Number() const { return number; }

	private:
	string name;
	int number;
};

//Function Declarations
void fillPhonebook(vector<Phonebook>&myPhonebook);
//fillVector
//@param vector<Class>)& - the vector to be filled



/*void printPhonebook(vector<Phonebook>&myPhonebook);
//printVector - prints the information of all students
//@param const vector<Phonebook>& - names and numbers in phonebook*/

//Function Declarations
void bubbleSort(vector<Phonebook>&myPhonebook);
// BubbleSort - Sorts vector
// @param myList - The vector to be sorted

void swap(Phonebook &p, Phonebook &q);
// Swap - Creates temporary variables for swapping Names

int i = 0;
int deleteFromVector;

int main()
{
	vector<Phonebook>myPhonebook;
	
	while(true)
	{
		cout << " - Press 1 to add a person and number" << endl;
		cout << " - Press 2 to print all persons and numbers" << endl;
		cout << " - Press 3 to delete person and number" << endl;
		cout << " - Press 4 to quit" << endl;

		int choice;
		cin >> choice;
	
		switch(choice)
		{

			case 1:
			{
				fillPhonebook(myPhonebook);
			}
			break;		
			case 2:
			{
				/* cout << "Print all names and numbers" << endl;
				printPhonebook(myPhonebook); */

				bubbleSort(myPhonebook);
				cout << "Sort order by age, youngest first:" << endl;
				for (int i = 0; i < myPhonebook.size(); i++)
				{
					cout << myPhonebook[i].Name() << " " << myPhonebook[i].Number() << endl;
				}

				cout << "\n";
			}
			break;		
			case 3:
			{
				int index;

				cout << "Which index do you want to delete? (1-10)" << endl;

				bubbleSort(myPhonebook);
				for (int i = 0; i < myPhonebook.size(); i++)
				{
					cout << i << " " << myPhonebook[i].Name() << " " << myPhonebook[i].Number() << endl;
				}

				cin >> index; 

				cout << "\n";

				myPhonebook.erase(myPhonebook.begin() + index);
      		}
			break;
			case 4:
				break;
		}

	}

	return 0;
}

// Function Definitions
void fillPhonebook(vector<Phonebook>&myPhonebook)
{
		string name;
		int number;					

		// Flush makes sure that the buffer is cleared and the characters are written to their destination
		cout << "Enter persons name: " << flush << endl;
		
		// Enter values into vector
		cin >> name;
			
		cout << "Enter persons number: " << flush << endl;
		
		// Enter values into vector
		cin >> number;

		/*Phonebook Phonebook(name, number);
		myPhonebook.push_back(Phonebook);*/

		myPhonebook.push_back(Phonebook(name, number));

		cout << endl;

		
			
}

void bubbleSort(vector<Phonebook>&myPhonebook)
{
	// The outer loop, going through all list
    for(int i = 0; i < myPhonebook.size(); i++)
	{
		//Inner loop, going through element by element
		int nrLeft = myPhonebook.size() - i; // To se how many has been gone through
		for (int j = 0; j < nrLeft - 1; j++)
		{
			
			if (myPhonebook[j].Name() > myPhonebook[j+1].Name()) // Compare the elements
			{
				swap(myPhonebook[j], myPhonebook[j+1]); 
			}
		}
	}
}

void swap(Phonebook &p, Phonebook &q) 
{ 
	Phonebook temp = p; 
	p = q;
	q = temp;
}
closed account (SECMoG1T)
The break statements within the switch can only function within the switch scope, since your loop is controlled by a bool literal true your loop will run infinitely , if you want to break from the loop you gotta use better controls and conditions such as if and else statements.

For eexample

1
2
3
4
5
6
7
8
9
10
11
12
13
bool running=true;

 while (running)
  {
     cin>> choice; 

      switch (choice)
     {
      ///more code
       if a particular condition is met 
            running=false;
      }
   }
Last edited on
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
int choice = 0 ;

while( choice != 4 )
{
    // std::cout << ...
    std::cin >> choice ;

    switch(choice)
    {
        case 1 :
            something() ;
            break ;

        case 2 :
            something_else() ;
            break ;

        case 3 :
            yet_another_thing() ;
            break ;

        case 4 :
            break ;

        default :
            std::cout << "invalid choice. try again\n" ;
    }
}
1
2
3
4
5
	case 4:
				break;
		}
break; // break after switch fix the issue
Topic archived. No new replies allowed.