Turning 4578 into "Four Thousand Five Hundred Seventy Eight"?

I want the user to enter a number between 0-9999 and then out put there number as a string that names that number, I assume that I should utilize arrays for this, but I can't seem to get anywhere with it, could someone give a suggestion or two to get started?

thanks in advance.
1
2
3
4
5
6
7
8
int main()
{
    int numInput;
    char convInput[5];
    cout << "Enter a number: ";
    cin >> numInput;
    itoa(numInput, convInput, 10); //converts int to char array
}


Start with that and see how far you can get.
Start with splitting the number into different digits
I take it that by converting the integer to an alphanumeric character array, I will be able to separate the numbers in the array:

1
2
3
4
5
//from int 
int number = 4567;

//to
char number[] = {4,5,6,7,\n};


is this a general idea of how to start off (using itoa to convert the number of course).
Last edited on
Yes, you could even save yourself the hassle and instead of converting the int to a char array just get the input from the user directly into the array. EX:

1
2
3
char number[5];
cout << "Enter a number: ";
cin >> number;


Then you can just test each character and it's position with a few switch or if statements and convert it to text. Or even easier might be to create a few arrays with the numbers in the same place as that number. EX:

1
2
char ones[20][20] = {"zero", "one", "two", "three", "four"..."eighteen", "nineteen"};
char tens[10][20] = {"zeroty", "tenty", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"}


then if the last number in the char array is 3 and the second to last number in the array is 0 then you could use: cout << ones[3]; and guess what gets output? (three)
one of the guidlines that I must follow are to use and integer variable, so I do have to use itoa.
however, I do see what your saying, but there is something that confuses me.

there is an example very similar to the arrays that you just now used in my programming book, but why are the arrays 2-dimensional?

could someone explain?
Last edited on
1
2
3
4
5
6
char a[5][20]; //creates 5 character arrays each of which can be 19 char's long
for(int k=0; k<5; k++)
{
     cout << "Enter a word: ";
     cin >> a[k];
}


Let's assume the user enters:
Hello
My
Name
Is
Joe

Then, cout << a[0]; outputs (Hello)
OR, cout << a[0][0]; outputs just (H)
OR, cout << a[3]; outputs (Is)
OR, cout << a[3][1]; outputs just (s)

The first set of []'s contains addresses to the beginning of the second set of []'s.
So a[0] Prints out all characters starting at that address whereas a[0][0] only prints out the first character found at that address.
Last edited on
Alright, that clears up some of my misunderstandings of 2-dimensional arrays, So how would the arrays that you mentioned earlier be utilized? I'm still not sure exactly how to work it.

sorry for the constant questions, but I'm desperate for help on this subject, and you've been a great help so far.
Here's the easiest way to do this without converting the int input to char array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
     //declare variables
     int getInt, thou, hund, ten, one, temp;
     char nums[20][20] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
     char tens[10][20] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
     //guts of the program
     cout << "Enter a number: ";
     cin >> getInt;
     //Convert thousands
     thou = getInt / 1000;
     if (thou > 0)
          cout << nums[thou] << " thousand ";
     getInt = getInt - (thou * 1000);
     //Convert hundreds
     hun = getInt / 100;
     if (hun > 0)
          cout << nums[hun] << " hundred ";
     //...I THINK YOU CAN TAKE IT FROM HERE
}
thank you, this really helps me get a better idea of what im working with, thanks for all the hlep
Alright, I got it working now, once again Thanks a lot
Awesome, glad you figured it out.
Topic archived. No new replies allowed.