Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,115 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,761 people online right now. Registration is fast and FREE... Join Now!




Trying to use any # of digits

 
Reply to this topicStart new topic

Trying to use any # of digits, stuck on 3 digits only

Laughing Man
11 Apr, 2007 - 12:03 PM
Post #1

New D.I.C Head
*

Joined: 11 Apr, 2007
Posts: 2


My Contributions
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
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Trying To Use Any # Of Digits
11 Apr, 2007 - 12:37 PM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,178



Thanked: 18 times
Dream Kudos: 450
My Contributions
Instead of reading in as a number, you could read it in as a string. Then store the integer values in an array or a vector.

CODE
string str;
vector <int> vec;
cin >> str;
for (int i = 0; i < str.length(); i++)
    vec.add(str.at(i) - '0');
cout << vec;



i don't know if os class overloads the '<<' operator for vectors, but if not you can just run through another for loop.

This post has been edited by GWatt: 11 Apr, 2007 - 12:40 PM
User is online!Profile CardPM
+Quote Post

Mickey
RE: Trying To Use Any # Of Digits
11 Apr, 2007 - 12:39 PM
Post #3

New D.I.C Head
*

Joined: 11 Apr, 2007
Posts: 7


My Contributions
Untested, but you should get the idea

CODE

int sum, temp;
bool first = true;

cout << "Enter number:  ";
cin >> sum;

temp = sum;

while( temp > 0 )
{
     if( !first)
          cout << " + ";
     else
          first = false;

     cout << temp%10;
     temp = temp / 10;
}

cout << " = " << sum << endl;


User is offlineProfile CardPM
+Quote Post

Laughing Man
RE: Trying To Use Any # Of Digits
11 Apr, 2007 - 05:08 PM
Post #4

New D.I.C Head
*

Joined: 11 Apr, 2007
Posts: 2


My Contributions
thanks alot guys but gwatt I'm not really sure what you mean I mean like your probably works but I haven't gone that far yet so not quite sure what vectors are and havent touched on array strings yet.

Mickey yours was more understandable and worked except in doesnt give me the sum rather just = to the number I had just entered 3456...3+4+5+6=3456 but thats ok I can work with that thanks abunch
User is offlineProfile CardPM
+Quote Post

Mickey
RE: Trying To Use Any # Of Digits
12 Apr, 2007 - 11:13 AM
Post #5

New D.I.C Head
*

Joined: 11 Apr, 2007
Posts: 7


My Contributions
Didn't notice you was adding them up there. It's simple enough to just add them up in the loop.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:51PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month