switch statment
Oct 17, 2012 at 9:37am UTC
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
#include<iostream.h>
main()
{
int num, r;
r = num / 10;
cout<< "enter number = " ;
cin>> num;
cout<< "\n" ;
switch (num)
{
case '0' : cout<< "zero" ;
break ;
case '1' : cout<< "one" ;
break ;
case '2' : cout<< "two" ;
break ;
case '3' : cout<< "three" ;
break ;
case '4' : cout<< "four" ;
break ;
case '5' :cout<<" five" ;
break ;
case '6' : cout<< "six" ;
break ;
case '7' : cout<< "seven" ;
break ;
case '8' : cout<< "eight" ;
break ;
case '9' : cout<< "nine" ;
break ;
default :
break ;
}
switch (r)
{
case '2' :cout<< "two" ;
break ;
case '3' :cout<< "three" ;
break ;
case '4' : cout<< "four" ;
break ;
case '5' :cout<<" five" ;
break ;
case '6' : cout<< "six" ;
break ;
case '7' :cout<< "seven" ;
break ;
case '8' :cout<< "eight" ;
break ;
case '9' :cout<< "nine" ;
break ;
default :
break ;
}
switch (num % 10)
{
case '1' : cout<< "one" ;
break ;
case '2' :cout<< "two" ;
break ;
case '3' :cout<< "three" ;
break ;
case '4' :cout<< "four" ;
break ;
case '5' :cout<<" five" ;
break ;
case '6' :cout<< "six" ;
break ;
case '7' :cout<< "seven" ;
break ;
case '8' :cout<< "eight" ;
break ;
case '9' :cout<< "nine" ;
break ;
default :
break ;
}
}
it is not working kindly identify the errror
Last edited on Oct 17, 2012 at 9:38am UTC
Oct 17, 2012 at 10:00am UTC
You are using '1','2'.....etc that's wrong because it's for the character...
For integer...
just write
1 2 3 4 5 6 7 8
case 1:
//cout your statement
break ;
case 2:
//cout your statement
break ;
...........................................
...........................................
so on...
Oct 17, 2012 at 3:36pm UTC
thanks dude
Oct 17, 2012 at 3:43pm UTC
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
#include<iostream.h>
main()
{
int num, r;
r = num / 10;
cout<< "enter number " ;
cin>> num;
cout<< "\n" ;
switch (num)
{
case 0: cout<< "zero" ;
break ;
case 1: cout<< "one" ;
break ;
case 2: cout<< "two" ;
break ;
case 3: cout<< "three" ;
break ;
case 4: cout<< "four" ;
break ;
case 5:cout<<" five" ;
break ;
case 6: cout<< "six" ;
break ;
case 7: cout<< "seven" ;
break ;
case 8: cout<< "eight" ;
break ;
case 9: cout<< "nine" ;
break ;
default :
break ;
}
switch (r)
{
case 2:cout<< "two" ;
break ;
case 3:cout<< "three" ;
break ;
case 4: cout<< "four" ;
break ;
case 5:cout<<" five" ;
break ;
case 6: cout<< "six" ;
break ;
case 7:cout<< "seven" ;
break ;
case 8:cout<< "eight" ;
break ;
case 9:cout<< "nine" ;
break ;
default :
break ;
}
switch (num % 10)
{
case 1: cout<< "one" ;
break ;
case 2:cout<< "two" ;
break ;
case 3:cout<< "three" ;
break ;
case 4:cout<< "four" ;
break ;
case 5:cout<<" five" ;
break ;
case 6:cout<< "six" ;
break ;
case 7:cout<< "seven" ;
break ;
case 8:cout<< "eight" ;
break ;
case 9:cout<< "nine" ;
break ;
default :
break ;
}
}
now if i enter one digit number it showing following output
when i enter 2 digit number it displaying last digit
the out put must be following
enter number 24
twentyfour
Oct 17, 2012 at 4:52pm UTC
Try to put your line:
r = num / 10;
AFTER
cin>>num;
You have it before, and if you do the division before receiving the user's input, num will have garbage data and the resulting value of r will be wrong.
Topic archived. No new replies allowed.