Picking out thousands groupings from an int
to display the ASCII value of is a rather strange task. You have "sow"
handled fine. You need to basically do the same thing in reverse for "wos"
. While there a many, many ways to handle the arithmetic, following on your form of and limiting the algorithm to one-loop and using a lookup-table for the divisor, you could do something similar to:
int test = 119111115,
thous[] = { 1, 1000, 1000000, 1000000000 };
for (int i = sizeof test - 1; i >= 0; i--) {
int div = thous[i],
result = test / div;
if (result)
std::cout << (char)result;
test -= result * div;
}
std::cout << '\n';
Which just uses div
as the divisor and loops 3
, 2
, 1
, and 0
as the exponent to divide test
by. It tests the result
and if it is non-zero then outputs the ASCII value of that grouping and then eliminates that value from test
.
Both "sow"
and "wos"
put together could be written:
#include <iostream>
int main (void) {
int test = 119111115,
thous[] = { 1, 1000, 1000000, 1000000000 };
/* sow */
for (; test > 0; test /= 1000)
std::cout << (char)(test % 1000);
std::cout << '\n';
test = 119111115; // wos
/* wos */
for (int i = sizeof test - 1; i >= 0; i--) {
int div = thous[i],
result = test / div;
if (result)
std::cout << (char)result;
test -= result * div;
}
std::cout << '\n';
}
Example Use/Output
$ ./bin/sow
sow
wos
This is just one way to do it. Let me know if you have questions.