I have to create a program that asks a person to enter a 2 digit number (10-99) and convert it into words. I've been working on this for days and I still don't see what I'm doing wrong! PLEASE HELP ME!!!
EXAMPLE:
27
twenty-seven

#include <iostream>
using namespace std;
int main()
{
	int num;
	int ones_digit = num%10;
	int tens_digit = num/10;

	cout << "Enter a two-digit number:\n";
	cin >> num;

	if (num>=20) && (num<100)
	{
		switch (tens_digit)
		{
			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;
			default:
				cout << " Error ";
		}

		switch (ones_digit)
		{
			case 0:
				cout << " ";
				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;
			default:
				cout << " Error ";
		}
	}

	if (num>=10) && (num=<19)
	{
		switch (num)
		{
			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;
			default:
				cout << " Error ";
		}
	}
	return 0;
}

Edited 5 Years Ago by WaltP: Added CODE Tags -- please use them!

You have some fundamental problems with your code especially the lack of a proper formating scheme..Also please use code tags.

#include <iostream>
using namespace std;
int main()
{
int num;

cout << "Enter a two-digit number:\n";
cin >> num;

int ones_digit = num%10;//preform these operations
int tens_digit = num/10;//after you get the value for num...not before


if ((num>=20) && (num<100))//if statement was wrong
{
switch (tens_digit)
{
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;
default:
cout << " Error ";
}

switch (ones_digit)
{
case 0:
cout << " ";
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;
default:
cout << " Error ";
}
}

if ((num >= 10) && (num <= 19))//if statement was wrong
{
switch (num)
{
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;
default:
cout << " Error ";
}
}
return 0;
}

I have to create a program that asks a person to enter a 2 digit number (10-99) and convert it into words. I've been working on this for days and I still don't see what I'm doing wrong! PLEASE HELP ME!!!

With what? You didn't bother telling us what the problem is.

Edited 5 Years Ago by WaltP: n/a

You're doing the calculations before you're getting the number, and since num will be 0 at that point, tens digit and ones digit will always equal zero, no matter what. You need to take in your number THEN do the calculations for tens digit and ones digit.

#include <iostream>
#include <windows.h>
#include <math.h>
#include <cmath>
#include <string>
#include <sstream>

using namespace std;

	int num;
	int ones_digit;
	int tens_digit;

int main()
{	
    cout<<"Enter 1 or 2 digits\n\n";
    cin>> num;
    cin.ignore();
    
    string num2;
    stringstream out;
    out << num;
    num2 = out.str();
    num2.length();
    
    ones_digit = num%10;
    tens_digit = num/10;
    
    
    if ((num >= 0) && (num <=19))
    {
       switch(num)
             {
                               case 0: cout<<"Zero";
                               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;
                    
                    default: cout<<"Error";
                    break;
             }
    }
    else if((num2.length() == 2) && ((num >= 20) && (num <=99)))
    {
    switch(tens_digit)
    {
                       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;
    }
            switch (ones_digit)
		{
			case 0:
				cout << " ";
				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;
			default:
				cout << "Error";
       }
    }
    
    cout<<"\n";
    system("Pause");
    return 0;
}
#include<iostream.h>
#include<conio.h>
void main()
{
int n;
clrscr();
cout<<"\nEnter the number...;
cin>>n;
switch(n)
{
case1:cout<<"\nThe number is one";break;
case2:cout<<"\nThe number is two";break;
case3:cout<<"\nThe number is three";break;
default:cout<<"\nInvalid number";
}
getch();
}

What Red Goose said. This bit of code:

int ones_digit = num%10;
int tens_digit = num/10;

Needs to be AFTER you get "num" from cin. You may have other problems, but that was as far as I looked. Also, you may want to verify that it is a number and a number in the range specified.

Edited 2 Years Ago by rubberman

@ Red Goose
Your comment "You're doing the calculations before you're getting the number, and since num will be 0 at that point, tens digit and ones digit will always equal zero, no matter what." is not quite correct. As it is currently coded, num will be random garbage (whatever is on the stack at that point), so it may not be zero. As a result, the ones_digit and tens_digit values will definitely be some random number, unless this is running in a debugger! :-) Caveate Programmer! Using unitialized variables is always a roll-of-the-dice, so to speak!

This article has been dead for over six months. Start a new discussion instead.