I have a large piece of code :

string num_to_text[] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };

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

string power_to_text[] = { "", "thousand", "million", "billion" };

string padIfNeeded (string ans)
{
        if ( ans == "" )
        {
                return "";
        }
        return " " + ans;
}

string translateHundred (int hundred_chunk)
{
        // handle special cases in the teens
        if ( hundred_chunk < 20 ) {
                return num_to_text[ hundred_chunk ];
        }
        int tens = hundred_chunk / 10;
        int ones = hundred_chunk % 10;
        return tens_to_text[ tens ] + padIfNeeded( num_to_text[ ones ] );
}


string translateThousand (int thousand_chunk)
{
        if ( thousand_chunk < 100 )
        {
                return translateHundred( thousand_chunk );
        }
        else
        {
                int hundreds = thousand_chunk / 100;
                int hundred_chunk = thousand_chunk % 100;
                return num_to_text[ hundreds ] + " hundred" + padIfNeeded( translateHundred( hundred_chunk ) );
        }
}


int main()
{
        int n;
        cin >> n;
        string number;
        bool is_negative = false;
        if ( n < 0 ) 
        {
                is_negative = true;
                n *= -1;
        }

        int chunk_count = 0;
        while ( n > 0 )
        {
                if ( n % 1000 != 0 ) {
                        number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );
                }
                n /= 1000;
                chunk_count++;
        }       
        if ( number == "" )
        {
                number = "zero";
        }
        if ( is_negative )
        {
                number = "negative " + number;
        }
        cout << number << endl;

the code is very simple and easy but there is only something in the following structure that confuses me a lot :

while ( n > 0 )
            {
                    if ( n % 1000 != 0 ) {
                            number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );
                    }
                    n /= 1000;
                    chunk_count++;

the code give the name of a number in words.

the first part is divided into three parts

1) it ensures white spaces between words when the number is given in words

2) it gives numbers from 1 to 99

3) it gives numbers from 1 to 999

the second is where the program begins and where the first part is called by the main function

what I could not understand is:

number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );

the firt "translateThousand

( n % 1000 )

" gives any number between 1 and 999
power_to_text[ chunk_count ] gives (billion or million or thousand) but the problem is with (number) because how comes that :

**number= something + number when considering that the (+) is for concatenation between strings and not for arthmetic operation?????!!!!! **

in other words why it is written (number) at the end of :

number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );?????

2)what is the role of

(n/=1000)

in this context????

3) and consequently how the whole piece of code works???? I mean:

while ( n > 0 )
                {
                        if ( n % 1000 != 0 ) {
                                number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );
                        }
                        n /= 1000;
                        chunk_count++;

Recommended Answers

All 8 Replies

number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );

Basically each component here is a function returning a string. This means that instead of assigning the values to a variable first, you can use the values concatenated with other string functions or variables in an assignment statement.

thank you for your reply but I already know what you wrote to me. You explained to me what this piece of code :

number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );

does.

I know what it does.
My question was not "what the above code does?"

if we take the piece of code like this :

number = translateThousand( n % 1000 ) +  power_to_text[ chunk_count ] +  number ;

.

my question was about the meaning of the word number being a variable and in the same time a value (in concatenation context)

so I will ask it more clearly:

1)what the meaning of the word** number** being a variable and in the same time a value **(in concatenation context**)?????!!!

what is the explanation beyond itwith details and specifications???

the second question was not answered:

the meaning of

n /= 1000;

2)what is the meaning of

n /= 1000;

in the code context of course?????

3)consequently to the first and second questions

how does this whole piece of code :

  while ( n > 0 )
    {
    if ( n % 1000 != 0 )
    {
    number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );
    }
    n /= 1000;
    chunk_count++;




**work???????**

number is a variable but it has value. When you want to change that value you can use the existing value to calculate the new value. This works since the new value won't get assigned until the calculations are finished. Which means that the variable will have its old value until then.

n /= 1000;

This is the same as n=n/1000;

Basically that code appears to break the number down bit by bit and assign string values to the various parts. It's a bit difficult deciphering it since it's not programmed very efficiently and is somewhat spaghetti-ized. For instance padIfNeeded is run in several places. But, if the arrays used " " instead of "" that whole function isn't needed.

your explanation is very instructive and interesting but I have some requests concerning it.

"number is a variable but it has value. When you want to change that value you can use the existing value to calculate the new value"

this is true only and only if the variable , from the beginning , during its declaraction is assigned a value otherwise it is wrong is it??????:

for example

string number="world";

number = "hello" + number ;

the result is :

number= hello world

but if :

the variable , **from the beginning, has no value **

there will be no change because ,simply, there is no value to be changed

or if we take a look in our code we will see that

from the beginning, number is declared as a string but has no value.

so I still do not get the idea beyond :

"

number = translateThousand( n % 1000 ) +  power_to_text[ chunk_count ] + number" ;

why the word number in value and in the variable at the same time and what is the meaning beyond it????????

2) as for

n=n/1000;

you said :**"to break the number down bit by bit"
**
but when I made an experience on it I saw that it breaks the number but **not bit bit by bit otherwise like this:
**
for example:

12523 gives ,when broken down ,;

12
0

125238 gives

125

0

1252345 gives

125
0
so in each case , where is the rest of the number like 523 , 238 ,2345?????

one last thing :

could you , please , tell me what is the role of

chunk_count++

in our code

and how it scans through the string or the number ; how it works???????

Using your above example for 1252345

n = 1252345
number = ""
chunk_count = 0

number = 1252345 % 1000 = 345 <- convert to string
n      = 1252345 / 1000 = 1252
number = three hundred forty five + number
       = three hundred forty five
chunk_count = 1

number = 1252 % 1000 = 252 <- convert to string
n      = 1252 / 1000 = 1
number = two hundred fifty two thousand + number
       = two hundred fifty two thousand three hundred forty five
chunk_count = 2

number = 1 % 1000 = 1 <- convert to string
n      = 1 / 1000 = 0
number = one million + number
       = one million two hundred fifty two thousand three hundred forty five
chunk_count = 3 // not used as n = 0, so loop exits.

chunk_count is just used as an index into the power_to_text array.
string power_to_text[] = { "", "thousand", "million", "billion" };

The variable declaration string number; assigns an empty string to variable number, so in essence number = ""

thank you very much , it makes thing more easier to me .but I still have more questions:

 number = two hundred fifty two **thousand** + number


= two hundred fifty two thousand three 





number = one ** million** + number
= one million two hundred fifty two thousand three hundred forty five

normally it must be :

1) how did you get the million and the thousand????? in other words , how did the power_to_text[ chunk_count ] work so that the compiler knows where to put the thousand , the million and the billion without making mistakes by putting them in the wrong place or missing them????

2) why we do not put the :

 n /= 1000;
chunk_count++;

inside the if statement of the while loop like this:

while ( n > 0 )
{
if ( n % 1000 != 0 ) {
number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );
n /= 1000;
chunk_count++;
}

so that every time , it will loop , make the division and the operation of modulus and return the string numbern all in the same time??????

1) how did you get the million and the thousand????? in other words , how did the power_to_text[ chunk_count ] work so that the compiler knows where to put the thousand , the million and the billion without making mistakes by putting them in the wrong place or missing them????

At the first iteration, the chunk_count variable is 0, and so, the power_to_text[ chunk_count ] is the element 0 of the array power_to_text, which is "". At the second iteration, chunk_count is 1, and thus, power_to_text[ chunk_count ] is "thousand". And so on, for "million" and "billion". They will be placed in the correct place just because the iterations go in order, I mean, it's a loop. First, the base numbers, then the thousands, then the millions, and so on. That's all there is to explain here.

2) why we do not put the :

n /= 1000;
chunk_count++;

inside the if statement of the while loop like this:

The only reason why the if-statement exists is to avoid adding anything to the number string when there is no number to describe. For example, if you have 2000, then you don't print anything for the base numbers, only when you get to the thousands. The thing is, you need to divide n by 1000 and increment the chunk_count variable regardless of whether the previous (0,999) number was 0 or not. That's why those statements are outside the if-statement.

m sorry but i'm new to c++
i have done the basic functions loops arrays and some of the programs ...
may it is the reason i could't comprehend how this program goes...
can somebody please illustrate me the steps at beginner level
specially
`

 bool is_negative = false;
        if ( n < 0 ) ``
        {
                is_negative = true;
                n *= -1;
        }

`

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.