//Have to convert digits to words, having problems with the output. Please help.

#include<iostream>

using std::cout;
using std::cin;
using std::endl;

int numToText(int num)				//function to return text version of number entered
{
	switch (num/10)					//Nested switch, this part detemines if number is multimple of ten
	{
	case 1:
            switch (num)
            {						//Prints teen numbers in words
       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;
       }								//prints numbers 20 - 99 in words
       case 2:cout<<"Twenty-";
       break;
       case 3:cout<<"Thirty-";
       break;
       case 4:cout<<"Forty-";
       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 (num<10 || num>19)				//prints ones value in text of numbers 20-99
       {
	   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;
	   }}
	  return num;
	}

//Main function that ask user to enter a number then calls a function to print it in words.
int main()
{
	int num;
	cout<<"Enter a number less than 100: "<<endl;		//asks user to enter a number
	cin>>num;
	if ((num <1) || (num > 100))		//determines if number is within range, prints error message if it's not
	{
		cout<< "Incorrect number entered. Please enter a number between 1 and 100: \n\n"<<endl;
	}

	cout<<"\nYou entered the number "<<numToText(num);
//These 2 lines entered because when I'm using Visual c++ 5 the output screen flashes so I don't get to see
	//the output without this.
	char ch;  
	cin>>ch;
return 0;
}

Here's a sample output:

Enter a number less than 100:
56
Fifty-six
You entered the number 56

You need to add a break after case 1 because it just runs into case 2 which makes it output "twenty"

Here is another way to do this with strings and arrays.

using std::string;

int numToText(int num)                //function to return text version of number entered
{

    string ones[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
                    "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
    string tens[] = { "", "", "Twenty", "Thrity", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };

    if( num < 20 )
        cout << ones[num] << endl;
    else
    {
        cout << tens[num/10];
        if( num % 10 != 0 )
            cout << "-" << ones[num%10];
        cout << endl;
    }
}

Thanks for the heads up on the first break. I can't use strings and arrays yet (hasn't been taught in class) but will definitely retry when we get to that point.

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.