# Converting int to words using switch (doesn't convert properly)

Page 1 of 1

## 6 Replies - 4020 Views - Last Post: 30 May 2012 - 06:50 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=281149&amp;s=772d2bc058d0a69f507f500c68825e5f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Hypernal

Reputation: -1
• Posts: 125
• Joined: 29-May 12

# Converting int to words using switch (doesn't convert properly)

Posted 29 May 2012 - 05:19 PM

We were told to write a program that will convert int from 1-3000 to words using switches.
I was able to write it but it doesn't convert properly. The problem occurs when the user inputs
digits with 11-19.

For example:
Enter number: 2011
Output: Two thousand Ten One

Someone told me to use:
```if ((num >= 0) && (num <=19)){

```

But I can't figure it out where to put it.

```#include <iostream>
#include <conio.h>

using namespace std;

main(){
int num, ones, tens, hundreds, thousands;

cout<<"Enter a digit from 1-3000: ";
cin>>num;

thousands=(num/1000)%10;
hundreds=((num/100)%100)%10;
tens=(num/10)%10;
ones=num%10;

switch(thousands){
case 1:
cout<<"One Thousand ";
break;
case 2:
cout<<"Two Thousand ";
break;
case 3:
cout<<"Three Thousand ";
break;
}

switch (hundreds){
case 1:
cout<<"One Hundred ";
break;
case 2:
cout<<"Two Hundred ";
break;
case 3:
cout<<"Three Hundred ";
break;
case 4:
cout<<"Four Hundred ";
break;
case 5:
cout<<"Five Hundred ";
break;
case 6:
cout<<"Six Hundred ";
break;
case 7:
cout<<"Seven Hundred ";
break;
case 8:
cout<<"Eight Hundred ";
break;
case 9:
cout<<"Nine Hundred ";
break;
}
switch (tens){
case 1:
cout<<"Ten ";
break;
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;
}
switch (ones){
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;
}
getch();
}

```

Is This A Good Question/Topic? 0

## Replies To: Converting int to words using switch (doesn't convert properly)

### #2 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Converting int to words using switch (doesn't convert properly)

Posted 29 May 2012 - 07:09 PM

One problem with that is, if you use an if you're not using a switch.

```       switch (tens){
case 1:
cout<<"Ten ";
break;

```

Isn't that where you need a different approach?

### #3 Hypernal

Reputation: -1
• Posts: 125
• Joined: 29-May 12

## Re: Converting int to words using switch (doesn't convert properly)

Posted 29 May 2012 - 07:54 PM

I managed to solve the 11-19 digits but on some cases. it doesn't read the last two digits.

Example:
Enter a number from 1-3000: 2345
Output: Two Thousand Three Hundred

```#include <iostream>
#include <conio.h>

using namespace std;

main(){
int num, ones, tens, hundreds, thousands;

cout<<"Enter a number from 1-3000: ";
cin>>num;

thousands=(num/1000)%10;
hundreds=((num/100)%100)%10;
tens=(num/10)%10;
ones=num%10;

switch(thousands){
case 1:
cout<<"One Thousand "<<tens;
break;
case 2:
cout<<"Two Thousand ";
break;
case 3:
cout<<"Three Thousand ";
break;
default:
cout<<"Invalid ";
break;
}

switch (hundreds){
case 1:
cout<<"One Hundred ";
break;
case 2:
cout<<"Two Hundred ";
break;
case 3:
cout<<"Three Hundred ";
break;
case 4:
cout<<"Four Hundred ";
break;
case 5:
cout<<"Five Hundred ";
break;
case 6:
cout<<"Six Hundred ";
break;
case 7:
cout<<"Seven Hundred ";
break;
case 8:
cout<<"Eight Hundred ";
break;
case 9:
cout<<"Nine Hundred ";
break;
}

if ((tens == 1) && (ones >= 1)){

switch (ones){
case 1:
cout<<"Eleven ";
break;
case 2:
cout<<"Twelve ";
break;
case 3:
cout<<"Thirteen ";
break;
case 4:
cout<<"Fourteen ";
break;
case 5:
cout<<"Fifteen ";
break;
case 6:
cout<<"Sixteen ";
break;
case 7:
cout<<"Seventeen ";
break;
case 8:
cout<<"Eighteen ";
break;
case 9:
cout<<"Nineteen ";
break;
}
}

else if ((tens >= 1) && (ones == 0)){

switch (tens){
case 1:
cout<<"Ten ";
break;
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;
}

}

else if ((tens == 0) && (ones >= 1)){

switch (ones){
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;
}
}
getch();
}

```

### #4 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Converting int to words using switch (doesn't convert properly)

Posted 29 May 2012 - 08:03 PM

You should be able to see that you're overlooking a large group of cases.

But also, with this approach your code is going to become very repetitive.

### #5 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: Converting int to words using switch (doesn't convert properly)

Posted 30 May 2012 - 01:52 AM

You can change the code from line 96 onwards to something like
```       else{
if (tens >= 1){

switch (tens){
case 1:
cout<<"Ten ";
break;
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;
}

}
if (ones >= 1){

switch (ones){
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;
}
}
getch();
}

```

### #6 Hypernal

Reputation: -1
• Posts: 125
• Joined: 29-May 12

## Re: Converting int to words using switch (doesn't convert properly)

Posted 30 May 2012 - 02:34 AM

Wow! That solves it! All this time, I forgot to use nested if/else. Thank you very much!

### #7 baavgai

• Dreaming Coder

Reputation: 7150
• Posts: 14,890
• Joined: 16-October 07

## Re: Converting int to words using switch (doesn't convert properly)

Posted 30 May 2012 - 06:50 AM

First, don't use switches. However, if you've been told to use switches, then use them. An if-then is cheating.

Always use functions. So:
```std::string getOnes(int ones) {
switch (ones) {
case 1: return "One";
case 2: return "Two";
case 3: return "Three";
case 4: return "Four";
case 5: return "Five";
case 6: return "Six";
case 7: return "Seven";
case 8: return "Eight";
case 9: return "Nine";
}
return "!";
}

void showText(int num) {
int thousands = num/1000;
switch(thousands){
case 0:
break;
case 1:
case 2:
case 3:
cout << getOnes(thousands) << " Thousand ";
break;
default:
cout << "Invalid ";
return;
}

num -= thousands*1000;
int hundreds = num/100;
switch(hundreds){
case 0:
break;
default:
cout << getOnes(hundreds) << " Hundred ";
}

}

```

You seem to consistently forget about zeros and cases <1000.

Hope this helps.