Beginner programmer 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";
}
So it looks like the code you have already is able to print the numbers from 0-999 and you just need to expand it to handle 0-9,999?

You basically just need to make a few changes to support the extra digit.

1) Update your digits array to handle 4 numbers (since >= 1000 is 4 digits)

2) Update your getDigits function to start at index 3 (again, since the digits array is now 4 elements 0,1,2,3)

3) Update printWordsForNum
Since your number is stored in the digits array, the highest order number is at index 0.
ie. The number 3456 is stored in digits[] in the following way:
digits[0] == 3 //thousands
digits[1] == 4 //hundreds
digits[2] == 5 //tens/teens
digits[3] == 6 //units

Because the digits are stored like this, you need to add the thousands printout for digits[0], and then shift the rest of the indexes for the rest of the printouts.

You don't have to split it into array, just find multiplier (x=1,x*=10 until x is the most that <= number).
And work step right and check until 10 <= number <= 20 and print formatted. or else print as normal.

With this, your code will be very simple and short.
??? I'm not sure I understand what you are suggesting terapaht. I suspect the code as it is in the original post is what handoftheking was given and asked to modify to handly the thousands digit. I think the purpose is to understand how arrays work, and how to modify existing code. I don't think a complete rewrite would fly in this case.
Last edited on
thanks discofire. that helped a lot. The changing the index in the getDigits function was what I couldn't figure out
Topic archived. No new replies allowed.