Programming Assignment

I have to create a program that asks a person to enter numbers (1-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!
I get an error on all the case numbers and I dont understand what is wrong.
EXAMPLE:
  27
   
twenty-seven    


using namespace;
#include <string>

//function prototypes go here

int main()
{
int input;

cout << "Please enter a positive integer: ";
cin >> input;

cout << intName(input) << endl;

cout << endl;
system("pause");
return EXIT_SUCCESS;
}

string intName(int number)
{
int part = number; //The part that still needs to be converted
string name; //The return value

if (part >= 100)
{
name = digitName(part / 100) + " hundred";
part = part % 100;
}

if (part >= 20)
{
name = name + " " + tensName(part);
part = part % 10;
}
else if (part >= 10)
{
name = name + " " + teenName(part);
part = 0;
}

if (part > 0)
{
name = name + " " + digitName(part);
}

return name;
}


/*
Turns a digit into its English name
@param an integer btw 1 and 9
@return the name of digit( "one" ... "nine" )
*/
string digitName(int 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 ";
}

/*
Turns a number btw 10 and 19 into its English name.
@param number an integer btw 10 and 19
@return the name of the given number( "ten" ... "nineteen")
*/
string teenName(int number)
{
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 ";

}

/*
Gives the name of the tens part of a number btw 20 and 99
@param number an integer btw 20 and 99
@return the name of the tens part of the number( "twenty" ... "ninety")
*/
string tensName(int number)
{
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;
}
Last edited on
Hi,

You aren't doing the switch properly, namely they aren't including the keyword switch !!

Near the bottom of this page:

http://www.cplusplus.com/doc/tutorial/control/

Always put a default case in the switch to catch invalid input.
Also, your functions expect you to return a string, yet you are not returning anything. You are only printing.
1
2
3
4
5
6
7
8
9
10
string teenName(int number)
    switch (number)
    {
      case 10:
        return "Ten";
      case 11:
        return "Eleven";
      // etc.
    }
}
Last edited on
Another way to handle this might be to input the number as a string, then process it accordingly, e.g.:

1
2
3
4
5
6
7
8
9
10
        std::string number;  //input number as a string
        std::cin >> number;

        char *c_string;    //c string to hold the number after its converted to array
        c_string = new char[number.length()];

        for (int index = 0; index < number.length(); index++){  //push number onto c-string
            c_string[index] = number[index] - '0';                       //convert from ascii to int
        }
Topic archived. No new replies allowed.