Ok hey all new to forums and C++...wasn't sure how to title this sorry trying to learn and catch up since I missed first classes of my C++ class due to being transferred into it..anyways my problem is this: I have setup a program to were it can add up any 3 digits and give me a sum my problem is I can't figure out how I am able to change it to were I can input any amount of digits heres what I have so far.
CODE
# include <iostream>
using namespace std;
int main()
{
int num;
int sum;
int hundreds;
int tens;
int ones;
int temp;
cout << "Enter a number consisting of ? digits : ";
cin >> num;
cout << num << endl;
hundreds = num / 100;
temp = num % 100;
tens = temp / 10;
ones = temp % 10;
sum = hundreds + tens + ones;
cout << hundreds << " + " << tens << " + " << ones << " = " << sum;
return 0;
This right here works for any 3 digits and gives me a sum but if I add more like 3456 it will display 34+5+6 =45 so yah a co worker of mine said I may have to use a loop but I'm still trying to work that out. I need to be able to display any number of digits like 34567 and can display it to 3+4+5+6+7
Can anyone help me out point me in the right direction
Thanks