0

I've been given an assignment where we must print out an integer digit by digit in english form using a recursive method. E.G. 534 prints out "five three four".

This is what I've got:

int englishInt(int num) {
    if(num < 10) {
        switch(num) {
            case 0: cout << "zero ";
            case 1: cout << "one ";
            case 2: cout << "two ";
            case 3: cout << "three ";
            case 4: cout << "four ";
            case 5: cout << "five ";
            case 6: cout << "six ";
            case 7: cout << "seven ";
            case 8: cout << "eight ";
            case 9: cout << "nine ";
        }
    } else
        return englishInt(num / 10);
}

For some reason it's printing out the lowest digit to the highest digit in English...Shouldn't this keep returning until it reaches the first digit then print it out, and then print out each digit up as the stack unwinds?

David Kiger
  • 1,958
  • 1
  • 16
  • 25
Taylor Bishop
  • 479
  • 2
  • 5
  • 18

5 Answers5

4

Think about it with an example, say 537.

englishInt(537) -- prints nothing, calls
    englishInt(53) -- prints nothing, calls
        englishInt(5) -- prints "five six seven eight nine"

The reasons being:

  1. You don't have any break statements in your cases.
  2. You don't print anything out if num >= 10.
David Kiger
  • 1,958
  • 1
  • 16
  • 25
2

This happens because your method does not do anything in the invocations where you recurse down. You should do a print in each invocation, but recurse down only when the num is not zero.

The algorithm (in pseudocode) should be as follows:

englishInt(int num) {
    if (num != 0) {
        englishInt(num/10)
    }
    cout << english letter for (num % 10)
}

You need a special case for num == 0, so that calling englishInt(0) would produce some output.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

First, the minimal change I see to fixing your code.

  1. Add a break after each output in your switch.
  2. Recurse prior to the output, and always output after.

Therefore,

void englishInt(int num)
{
    if (num >=10)
        englishInt(num/10);

    switch(num % 10)
    {
        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;
    }
}

Ditch The Switch

I'm not following why you have a switch statement in the first place. If you look at your switch you're always evaluating a number between 0..9. So why not use that number for a simple index into an array of ten strings:

#include <iostream>
using namespace std;

void print_num(unsigned int num)
{
    static const char *strs[] =
    {
        "zero", "one", "two", "three", "four",
        "five", "six", "seven", "eight", "nine"
    };

    if (num >= 10)
        print_num(num/10);
    cout << strs[num % 10] << ' ';
}

int main(int argc, char *argv[])
{
    print_num(100); cout << endl;
    print_num(12345);  cout << endl;
    print_num(3);  cout << endl;
    print_num(1024*1024*1024); cout << endl;
    return 0;
}

Output

one zero zero 
one two three four five 
three 
one zero seven three seven four one eight two four 
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • It's a homework assignment -- they probably haven't covered look-up tables yet. – George Skoptsov Feb 25 '13 at 05:17
  • @kTekkie I got the feeling that as the point of the recursion in the first place. The start of the question states: "534 prints out 'five three four'.", so I'm assuming the order is probably desired from left to right. In fact, reading the comment after the OP's code, I'm sure of it. – WhozCraig Feb 25 '13 at 05:26
  • @GeorgeSkoptsov Probably not, nor have they apparently covered the "break" statement in a switch. – WhozCraig Feb 25 '13 at 05:27
  • Yeah, not including the break in the switch was a huge oversight on my part.Thanks guys! I'm going to look up look-up tables now – Taylor Bishop Feb 25 '13 at 05:43
  • @TaylorBishop Glad to help. See above for a very simple lookup table (a static list of string pointers indexed by the modulo of our current num value with 10). – WhozCraig Feb 25 '13 at 05:45
0

There are a couple of problems with your code.

Firstly, your switch statement is broken: you forgot to put a break statement after each case, so at the end of recursion your program will print out the most significant digit and then iterate through the greater digits up to nine.

Fix it as follows:

   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;
    }

Secondly, your program will have an output only for the most significant digit. You don't print anything for the intermediate steps. It's not a problem with your code, but with your algorithm, so you'll have to fix that on your own.

George Skoptsov
  • 3,831
  • 1
  • 26
  • 44
-1

A wild method to do

int main()
{
   int n;
   char s[32];
   char word[10][10]{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

   cout << "enter a number" << endl;
   cin >> n;

   sprintf(s,"%d", n );

   for( int i = 0; s[i] != '\0'; i++)
   {
       cout << word[s[i]-'0'];
   }

   return 0;
}
Riskhan
  • 4,434
  • 12
  • 50
  • 76