Beginner in need of assistance

I'm in an introductory programming class. We learning C++ and I have program that prints out the written version a number 0-999(i.e. enter 6 and the program prints out six). I have to make it able to do numbers up to 9,999. Can anyone help me? I have no prior programming experience so I'm honestly out of my depth.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
using namespace std;

int getNum();
void getDigits(int num, int digits[]);
void printWordsForNum(int digits[]);


int main()
{
  int  num, digits[3];

  while(true){          // Keep looping endlessly; could use 1 instead of 'true'
     num = getNum();
     if(num < 0)        // User said to stop
        return 1;       // End loop, end program
     getDigits(num, digits);
     printWordsForNum(digits);
  }
}

int getNum()
{
   int num;

   cout << "Enter integer (0 to 999;  -1 to quit): ";
   cin >> num;
   return num;
}

void getDigits(int num, int digits[3])
{
  int i;

  for(i = 2; i >= 0; --i){
     digits[i] = num % 10;
     num /= 10;
  }
}

void printWordsForNum(int digits[3])
{
  string 
     units[10] = {"zero","one","two","three","four","five","six","seven",
                                                              "eight","nine"},

     teens[10] = {"ten","eleven","twelve","thirteen","fourteen","fifteen",
                                "sixteen", "seventeen","eighteen","nineteen"},

     tens[10]  = {"","","twenty","thirty","forty","fifty","sixty","seventy",
                                                   "eighty","ninety"};

  if(digits[0] > 0){                                       // Example: 276
     cout << "\n" << units[digits[0]] << " hundred";       // 'two hundred '
     if(digits[1] + digits[2] > 0)
        cout << " and ";                                   // ' and '
  }

  if(digits[1] > 1)
     cout << tens[digits[1]] << " ";                       // 'seventy '

  if(digits[1] == 1)
     cout << teens[digits[2]] << "\n\n";                  
  else
     if(digits[2] > 0  ||  digits[0] + digits[1] == 0)
        cout << units[digits[2]] << "\n\n";                // 'six'
     else
        cout << "\n\n";
}
And you post it to beginners' too =..=
Topic archived. No new replies allowed.