Switch Case

helo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int ch
do
{
cout<<"\n ENter your choice";
cout<<"\n 1 2 3";
cin>> ch;
switch(ch)
{
case 1: cout<<"One";
break;
case 2: cout<<"two";
break;
case 3:exit (0);
default:cout<<"Not a valid choice";
break;
}
}whiel(!(ch==3));

in the above code whenever i enterd alphabet program goes infinite. i have also tried isdigit() function but can't run correctly...
any one help me?
Program gives error...
missing break in case 3, but that should not let your program go infinite...
do you want to stop the loop? or exit the application?
Last edited on
Which program and what error?
you can use ASCII
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char ch;
do
{
cout<<"\n ENter your choice";
cout<<"\n 1 2 3";
cin>> ch;
switch(ch)
{
case '1': cout<<"One";
break;
case '2': cout<<"two";
break;
case '3':exit (0);
default:cout<<"Not a valid choice";
break;
}
}while(!(ch=='3'));
Last edited on
but how can i get the ascii value of entered character?
Your variable ch contains ASCII value. Try for example

cout << static_cast<int>( ch );

and you will see ASCII value.
i have write statement
cout<<"\n ASCII value of choice "<<static_cast<int>(ch);
but it gives two errors like

Undefined symbol 'static_cast'
Expression syntax

both errors comes on above line. Is there anything that is to be required to include in program?
btw, why don't you:

1
2
3
4
5
6
7
8
9
10
int ch;
do {
	cout << "enter: ";
	cin >> ch;

	switch (ch) {
	case 1: cout << "one" << endl; break;
	case 2: cout << "two" << endl; break;
	default: cout << "error..." << endl; break;
} while ( !(ch == 3));


haven't compile that yet, but, i guess that should work...
No it doesn't work.program goes infinite....
Undefined symbol 'static_cast'
That's very wrong. What compiler are you using?

program goes infinite
I tried to give you a solution, but that didn't work..
Here's how it is. cin (or any stream) object contains an error flag. Amongst other situations this flag is set when you want to read a number, but there aren't any in the stream. When the flag is set, all IO operations are made unavailable until the error is handled. Handling such error would mean two things. Clear the error flag and remove the offending characters from the stream. You check the flag using fail() method (note that there are more). An interesting thing is that if(cin) is the same as if(!cin.fail()). I will not discuss how that works here.. To clear the flag, use method clear(). Removing garbage is not as nice. You could write cin.ignore(numeric_limits<streamsize>::max(), '\n'); where numeric_limits<streamsize>::max() needs <limits> included and is just a big number (you'd be fine if you used 100 instead). You could also read a dummy string with getline, although that's wasteful, unless you want to tell the user exactly what the problem was.

In code:
1
2
3
4
5
if(cin >> var) //do things
else {
   cin.clear();
   cin.ignore(100, '\n');
}
Last edited on
i can't understand what are you saying?
You'll have to be more specific. I'm not going to rephrase all that paragraph.
Topic archived. No new replies allowed.