6 Replies - 1892 Views - Last Post: 30 May 2012 - 06:50 AM Rate Topic: -----

#1 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • 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.

Think about this part of your program:

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



Isn't that where you need a different approach?
Was This Post Helpful? 0
  • +
  • -

#3 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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();
       }
                                 



Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • 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();
}     


Was This Post Helpful? 0
  • +
  • -

#6 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5881
  • View blog
  • Posts: 12,758
  • 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 ";
	}
	
	// your code here
	

}



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

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1