Help with Switch..

i want to write a C++ code using switch to ouput the equivalent ASCII value of digit/character entered by the user.
Sample Run:
enter a  digit/character: 5

its AScii value is 53


sample run:
enter a digit/character: @
Invalid Input


my code is :

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

int main()
{
  int x;
  char ch;

  cout<<"enter a decimal/character"<<endl;
  cin>>ch;
  if ( ch>='0' && ch<='9') 
	  x=0;
  if (ch>='a' && ch<='z' || ch>='A' && ch<='Z')
	  x=1;
  else
	  x=2;
  switch (x)
  {
  case 0:
	  cout<<"its AScii value"<<static_cast<int>(ch)<<endl;
	  break;
  case 1:
cout<<"its AScii value"<<static_cast<int>(ch)<<endl;
break;
  default:

	  cout<<"invalid input"<<endl;
  }
  return 0;
}


when i enter a digit.. it output to me
Invalid input

but when i enter a character it works.!!!??
wts the problem
Because if it's not a character you always set x to 2.
1
2
3
4
if (ch>='a' && ch<='z' || ch>='A' && ch<='Z')
	  x=1;
  else
	  x=2;
else if (ch>='a' /* ... */) x = 1;
Last edited on
Topic archived. No new replies allowed.