using exception on switch case

I get an infinite loop if i enter a char/string during the switch statement, so i made an exception to catch anything other then integers (line 155). But, i still get the infinite loop. I may be doing something wrong so any help would be great.

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
#include <iostream>
#include <string>
#include <conio.h>
#include <exception>
#include <typeinfo>
using namespace std;

class list
{
private:
	struct node
	{
		string data;
		node *next;
	}*head, *tail;

public:
	list() : head(0), tail(0) {}
	~list();
	void push_front(string &d);
	void push_back(string &d);
	bool isempty() const;
	bool pop_back(string &d);
	bool pop_front(string &d);

	friend ostream &operator<<(ostream &, list &);
};

list::~list()
{
	node *temp = head;
	node *tmp;

	while(temp != 0)
	{
		tmp = temp;
		temp = temp->next;
		delete tmp;
	}
}

bool list::isempty() const
{
	return head == 0;
}

void list::push_front(string &d)
{
	node *temp = new node;
	temp->data = d;
	temp->next = 0;

	if(isempty())
		head = tail = temp;
	else
	{
		temp->next = head;
		head = temp;
	}
}

void list::push_back(string &d)
{
	node *temp = new node;
	temp->data = d;
	temp->next = 0;

	if(isempty())
		head = tail = temp;
	else
	{
		tail->next = temp;
		tail = temp;
	}
}

bool list::pop_front(string &d)
{
	if(isempty())
		return false;
	else
	{
		node *temp = head;

		if(head == tail)
			head = tail = 0;
		else
			head = head->next;

		d = temp->data;
		delete temp;
		return true;
	}
}

bool list::pop_back(string &d)
{
	if(isempty())
		return false;
	else
	{
		node *temp = tail;

		if(head == tail)
			head = tail = 0;
		else
		{
			node *current = head;

			while(current->next != tail)
				current = current->next;

			tail = current;
			current->next = 0;
		}

		d = temp->data;
		delete temp;
		return true;
	}
}

ostream &operator<<(ostream &output, list &L)
{
	list::node *temp = L.head;

	while(temp != 0)
	{
		output << temp->data << endl;
		temp = temp->next;
	}

	return output;
}

int main()
{
	list LL;
	int choice;
	string name;
	int temp;

	try
	{
		do
		{
			cout << "Enter choice: " << endl;
			cout << "1. Push_front." << endl;
			cout << "2. Push_back." << endl;
			cout << "3. Pop_front." << endl;
			cout << "4. Pop_back." << endl;
			cout << "5. Quit." << endl;
			cout << "$ ";
			cin >> choice;
			if(typeid(choice) != typeid(temp))
				throw;
			else
			{
				switch(choice)
				{
				case 1:
					cout << "Enter name to save" << endl;
					cout << "$ ";
					getline(cin, name);
					getline(cin, name);
					LL.push_front(name);
					cout << LL;
					break;

				case 2:
					cout << "Enter name to save" << endl;
					cout << "$ ";
					getline(cin, name);
					getline(cin, name);
					LL.push_back(name);
					cout << LL;
					break;

				case 3:
					LL.pop_front(name);
					cout << LL;
					break;

				case 4:
					LL.pop_back(name);
					cout << LL;
					break;

				case 5:
					break;
				}
			}
			
		}while(choice != 5);
	}catch(...)
	{
		cout << endl << endl;
		cout << "error... the choice is not on the system." << endl;
		_getch();
	}

	return 0;
}
you've declared both choice and temp as int therefore the expression if(typeid(choice) != typeid(temp)) will always be false.

typeid is an operator which determines the static type of a variable at compile time, it has no knowledge of anything you do at runtime (such as user input), so it can't help you.

If you use cin >> choice and the user types in something which won't fit into an int variable, then the internal "fail" flag which belongs to cin will be set to true, and you'll not be able to do anything with cin until you've cleared the error state and discarded whatever bad data was waiting for you.

One option would be to perform your check as you're getting the data with if( cin >> choice ) then if it fails, use a combination of clear() to reset the state and ignore() to discard any bad data left waiting for you:
1
2
std::cin.clear();
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


Although letting cin fail in the first place usually isn't ideal IMO; A better way would be to use a string, which is capable of storing any kinds of characters (cin is less likely to fail if you're getting a string), then do a checked conversion using a local stringstream object instead.
This has the advantage of avoiding the need to reset cin every time (it shouldn't matter if a local stringstream object has its fail flag set)

Example of a reliable checking/conversion function using a stringstream:
1
2
3
4
5
6
7
8
9
10
bool convert( const std::string& str, int& n )
{
    std::istringstream buffer( str );
    bool success = false;
    if( buffer >> n )
    {
        success = true;
    }
    return success;
} 
Last edited on
Thanks for the reply, i tried the if(!(cin >> choice)) way, but i got a big error. And im not sure how to implement the bool convert(const std::string& str, int& n); option. I understand how it works, but why would i enter a string and a number? also, i can't use strings in the switch case.
Nevermind i got the if(!(cin >> choice)) way to work. thank you for the help!
Topic archived. No new replies allowed.