hello... I am trying to convert 2 digit numbers to words: example 11= eleven. I saw some other entries on this but they dont include case or switch

so far I have:

#include <iostream>
using namespace std;

cout<<"Enter a two-digit number: ";

cin>>number;

if(number>99)
{
 cout<<"Number out of range";
 exit(0);
}


if(number==0)
{
 cout<<"Zero";
 exit(0);
}

temp=number;

digit=number%10;                    
last_two=digit;
number=number/10;
digit=number%10;
last_two=(digit*10)+last_two;

while(temp!=0)                
{
 temp=temp/10;
 digit++;
}

cout<<"You have entered the number ";


int num;
{
switch(num)
{
 case 0: 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;
 case 10: cout<<"Ten ";
         break;
 case 11: cout<<"Eleven ";
         break;
 case 12: cout<<"Twelve ";
         break;
 case 13: cout<<"Thirteen ";
         break;
 case 14: cout<<"Fourteen ";
         break;
 case 15: cout<<"Fifteen ";
         break;
 case 16: cout<<"Sixteen ";
         break;
 case 17: cout<<"Seventeen ";
         break;
 case 18: cout<<"Eighteen ";
         break;
 case 19: cout<<"Nineteen ";
         break;
}
return;
}

can someone help?

I see a disturbing lack of the main function, that just might be your problem. ;) Note that executable code needs to be in a function.

Here's an idea:
First check is your number teen, or not.
If it is teen number (1 is first digit) then check second digit and write:

//just for example
case 1: str = "Eleven"; break;
case 2: str = "Twelve"; break;
//...

if it's not teen number, then you have to check first digit and make str = "Fifty", or str = "Ninety", etc.
Then check second digit and add to str:
str += "Two", str+="Three", and so on...
HTH

Probably, all beginners write verbose and heavy-to-read codes. Don't complicate your as it is not so easy life with monstrous switches. Use this common approach:

const char* numeral[] = {
"", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve"
};
...
cout << numeral[num] << ' ';
...

Justin - what are lines 21-33 supposed to be doing? If you're trying to separate the units and the tens, you're over complicating the process.

ArkM- your approach is fine, for a small number of numbers. What about the twenties, thirties....?

Handle the special case of the teens

Then do the multiples of 10, if any (20, 30...)

Then handle the units value.

To Vmanes
I did not suggest the complete solution but I hope the proposed code fragment is a part of any solutions (and presents useful program case solution at all ;)).

hello... I am trying to convert 2 digit numbers to words: example 11= eleven. I saw some other entries on this but they dont include case or switch

well, here it is, if my number spelling is not quite correct...srry
a switch is still used, but it's much smaller than urs

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
	const char *digits[] = {"","one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
	const char *teen[] ={"ten","eleven", "twelve", "thirteen", "fourteen", "fiveteen", "sixteen", 
		"seventeen", "eighteen", "nineteen" };
	const char *big[] = { "", "", "twen", "thir", "four", "fif", "six", "seven", "eight", "nine" };
	

	if ( argc != 2 )
	{
		cout<<"cmd line argument req: the number to convert..."<<endl;
		return 1;
	}
	unsigned char nr = atoi( argv[1] );

	unsigned char nr = 33;
	if ( nr < 1 || nr > 99 )
	{
		cout<<"numbers between 1 and 99 accepted"<<endl;
		return 2;
	}

	unsigned char sdigit = nr % 10;
	unsigned char fdigit = nr / 10;

	char result[32]="";

	switch (fdigit)
	{
	case 0: strcat( result, digits[ sdigit ] );  break;
	case 1: strcat( result, teen[ sdigit ] );  break;
	default:
		strcat( result, big[ fdigit]);
		strcat( result, "ty");
		strcat( result, digits[ sdigit] );
		break;
	}

	cout<<result;

	return 0;
}

It's not ideal code but it works ;):

/// See http://www.corollarytheorems.com/Grammar/numeral.htm
std::string Numeral(int);
namespace {
const int
    BILLION = 1000000000,
    MILLION = 1000000,
    THOUSAND= 1000,
    HUNDRED = 100,
    TEN     = 10
    ;
const char* tens[] = {
    "", "", "twenty", "thirty", "fourty", "fifty",
    "sixty", "seventy", "eighty", "ninety"
    };
const char* teen[] = { // feel too lazy to glue ty/teen
    "", "one", "two", "three", "four", "five",
    "six", "seven", "eight", "nine", "ten",
    "eleven", "twelve",
    "thirteen", "fourteen", "fifteen", "sixteen",
    "seventeen", "eighteen", "nineteen"
    };
int trio(std::string& s, int sum, int unit, const char* what) {
    int res = sum % unit;
    int n = sum / unit;
    if (n) {
        s += Numeral(n);
        s += what;
        if (res)
            s += ' ';
    }
    return res;
}
const std::string zero("zero");
}
/// For 32-bit integers only!
std::string Numeral(int n) {
    if (n == 0)
        return zero;
    std::string s;
    if (n < 0) {
        n = -n;
        s = "minus ";
    }
    // For 64-bit code insert corr here
    n = trio(s,n,BILLION," billion"); // Why if n < ...
    n = trio(s,n,MILLION," million");
    n = trio(s,n,THOUSAND," thousand");
    n = trio(s,n,HUNDRED," hundred");
    if (n) { // < 100
        int t = 0;
        if (n >= 20) {
            s += tens[t=n/TEN];
            n %= TEN;
        }
        if (n) {
            if (t)
                s += '-';
            s += teen[n];
        }
    }    
    return s;
}

can u cr8 me program that words converted to numbers.(1-50) using switch. pls help me.

can you code a program that number convertred to words (1-1000000000) using switch case.. pls. help me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.