// Hagopian doing PPP6 ex 09

/*
9. Write a program that reads digits and composes them into integers. 
For example, 123 is read as the characters 1, 2, and 3. The program 
should output 123 is 1 hundred and 2 tens and 3 ones. The number 
should be output as an int value. Handle numbers with one, two, three, 
or four digits. Hint: To get the integer value 5 from the character 
'5' subtract '0', that is, '5'–'0'==5.
*/

#include <iostream>
#include <cmath>
#include <vector>
#include <string>
using namespace std;

int main() {
	char c{ 0 };
	int x{ 0 };
	//cout << "isdigit('z') = " << isdigit('z') << endl;
	while (cin.get(c) && isdigit(c)) {
		x *= 10;
		x += c - '0';	
		//cout << "x = " << x << endl;
	}
	cout << x << " is ";
	int placeVal = (int)log10(x), pow10{ 1 };
	for (int i = 0; i < placeVal; ++i) pow10 *= 10;
	vector<string> numberWords{ "ones","tens","hundreds","thousands" };
	while (placeVal>0) {
		cout << (x / pow10) % 10 << " " << numberWords[placeVal]
			<< " and ";
		--placeVal;
		pow10 /= 10;
	}
	cout << x % 10 << " " << " ones.";
}