I'm stuck, I'm told to "Write a program that takes a multi-digit integer and breaks it into single-digit numbers that
represent each of the places in that number. For example, if the user inputs the number
"1337", you would print "1 3 3 7"."
All I have so far is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int x;
cout << "Project 3" << endl;
cout << "Enter a number" << endl;
cin >> x >> endl;
pow(10,
}
I'd suggest initializing a std::string from the number. Then you can bind that string to a stringstream and extract one character at a time into an integer. There should be an article on stringstreams around here somewhere... http://www.cplusplus.com/articles/numb_to_text/
Alternatively, you could do it the icky way with c-style strings. You'd have to use the nonstandard itoa() though.
You could get the string using getline(cin,s) after declaring string s;
I think you would need to include the header <string> or <cstring>
You could then use a for loop to print out the string cout<<s[i]<<" ";
The by far easiest way to convert an int to string, is to use the brand new function std::itos. If your compiler already ships with it, that is. (It'll be in the upcoming standard, so it may not be there already.)
So something along those lines:
1 2 3
...
std::string s = std::itos(x);
for (int i = 0; i < s.size(); ++i) cout << s[i] << " ";