How to get digits from a user and use them?

I'm an extreme beginner taking a C++ class for the first time. I'm trying to meet the requirements of an assignment in which I must get a string of numbers like this: 1234567890 from the user. Then I must do math on the numbers (in this case I have to add all the digits). What is the easiest way to get these numbers into a format I can work with? I need something that reads the string from the user and dumps it into an array, but the array needs to be of the int type not the char type. I've read a lot of responses to other people asking similar questions but nothing I've seen seems to answer what should be a relatively simple thing to do...I would post code, but I'm not that far yet. I can write the for loops to deal with the array once I have it, but I need the input to array problem solved first.

Why don't you start with what you do know? In your program, instead of getting the input from the user, simply declare your own int array to use.
int myArray[3] = {1,2,3};
Then, write the loop that iterates through myArray and adds the values.

If you come back with some working code, and then ask how to fill the array with user inputted data, people will be much more likely to help. Just a thought. No one wants to do someone else's homework for them.
Read the number into a std::string. To convert the character representation of a number into the actual number, just subtract '0' from it, e.g.
1
2
char numch = '7'; //numch != 7
int num = numch - '0'; //num == 7 
Last edited on
LB, I wanna kiss you! That worked like a charm.
Topic archived. No new replies allowed.