can't do the hundreds
Mar 12, 2013 at 3:18pm UTC
i made a program that converts digit numbers into letter numbers, with 2 digit numbers i had no problem, but with 3 digits i can't figure out how to do it
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
#include <iostream>
using namespace std;
int main (){
int num;
cout << "enter a number: " ;
cin >> num;
int decimals=num%10;
int tens=num/10;
int hundredths=num/100;
if (num<10){
switch (num){
case 1 : cout << "one" << endl;
break ;
case 2 : cout << "two" << endl;
break ;
case 3 : cout << "three" << endl;
break ;
case 4 : cout << "four" << endl;
break ;
case 5 : cout << "five" << endl;
break ;
case 6 : cout << "six" << endl;
break ;
case 7 : cout << "seven" << endl;
break ;
case 8 : cout << "eight" << endl;
break ;
case 9 : cout << "nine" << endl;
break ;
default : cout << "error" << endl;
}}
if (num>=100&&num<1000){
switch (hundredths){
case 1 : cout << "onehundred" ;
break ;
case 2 : cout << "twohundred" ;
break ;
case 3 : cout << "threehundred" ;
break ;
case 4 : cout << "fourhundred" ;
break ;
case 5 : cout << "fivehundred" ;
break ;
case 6 : cout << "sixhundred" ;
break ;
case 7 : cout << "sevenhundred" ;
break ;
case 8 : cout << "eighthundred" ;
break ;
case 9 : cout << "ninehundred" ;
break ;
default : cout << " " ;
}
switch (tens){
case 2 : cout << "twenty" ;
break ;
case 3 : cout << "thirty" ;
break ;
case 4 : cout << "fourty" ;
break ;
case 5 : cout << "fifty" ;
break ;
case 6 : cout << "sixty" ;
break ;
case 7 : cout << "seventy" ;
break ;
case 8 : cout << "eighty" ;
break ;
case 9 : cout << "ninety" ;
break ;
default : cout << "error" ;
}
switch (decimals){
case 1 : cout << "one" << endl;
break ;
case 2 : cout << "two" << endl;
break ;
case 3 : cout << "three" << endl;
break ;
case 4 : cout << "four" << endl;
break ;
case 5 : cout << "five" << endl;
break ;
case 6 : cout << "six" << endl;
break ;
case 7 : cout << "seven" << endl;
break ;
case 8 : cout << "eight" << endl;
break ;
case 9 : cout << "nine" << endl;
break ;
default : cout << "error" << endl;
}}
if (num>=20&&num<100){
switch (tens){
case 2 : cout << "twenty" ;
break ;
case 3 : cout << "thirty" ;
break ;
case 4 : cout << "fourty" ;
break ;
case 5 : cout << "fifty" ;
break ;
case 6 : cout << "sixty" ;
break ;
case 7 : cout << "seventy" ;
break ;
case 8 : cout << "eighty" ;
break ;
case 9 : cout << "ninety" ;
break ;
default : cout << "error" ;
}
switch (decimals){
case 1 : cout << "one" << endl;
break ;
case 2 : cout << "two" << endl;
break ;
case 3 : cout << "three" << endl;
break ;
case 4 : cout << "four" << endl;
break ;
case 5 : cout << "five" << endl;
break ;
case 6 : cout << "six" << endl;
break ;
case 7 : cout << "seven" << endl;
break ;
case 8 : cout << "eight" << endl;
break ;
case 9 : cout << "nine" << endl;
break ;
default : cout << "error" << endl;
}}
if (num>9&&num<20){
switch (num){
case 10 : cout << "ten" << endl;
break ;
case 11 : cout << "eleven" << endl;
break ;
case 12 : cout << "twelve" << endl;
break ;
case 13 : cout << "thirteen" << endl;
break ;
case 14 : cout << "fourteen" << endl;
break ;
case 15 : cout << "fifteen" << endl;
break ;
case 16 : cout << "sixteen" << endl;
break ;
case 17 : cout << "seventeen" << endl;
break ;
case 18 : cout << "eighteen" << endl;
break ;
case 19 : cout << "nineteen" << endl;
break ;
default : cout << "error" << endl;
}}
system("pause" );
return 0;
}
Mar 12, 2013 at 3:21pm UTC
If you have more than 2 digits number you will have to use % 10 on the tens as well.
int tens = (num / 10) % 10;
Mar 12, 2013 at 5:34pm UTC
Not that it matters all that much, but I'd use "\n" instead of endl and avoid system("pause") in any serious programming.
Topic archived. No new replies allowed.