No need to break up the input string.
Code:
cin >> input;
const int min = 1000;
const int max = 9999;
String output = "";
int value = input;
if(value >= min && value <= max) {
for(int i = 1; value > 0; i *= 10) { // Use i to keep track of current column
if(i == 1) {
// Don't append for first column
output = (value % 10) + " * " + i;
}
else {
// Append previous output for all remaining columns
output = (value % 10) + " * " + i + " + " + output;
}
value /= 10; // Progressively chop off each column, should ignore remainder since it's integer division
}
cout << output << endl;
}
else {
cout << value << " is outside of required value range " << min << " <-------> " << max << endl;
}
I like this implementation since it scales very well should your boundary values ever change. Not something that's important given the scope of this project, but in a real-world situation it would be ideal. Pardon any syntax errors; I've been working mostly in Java these days and it's been a while since I've done any C++ work. You'll need to pretty it up as well, I can't be doing all the work. :wink:
[edit]
Danit it, Haelian beat me to the simpler approach. I think I still like how mine is done better.