Thread: c++ problem from a book I'm working through

  1. #1
    Registered User
    Join Date
    Mar 2008

    c++ problem from a book I'm working through

    I've been trying to teach myself C++ through a book I picked up but this problem has me stumped ( its not homework):

    Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as 3 4 5 6; output the individual digits of 8030 as 8 0 3 0; output the individual digits of -2345 as 2 3 4 5.

    heres the code I have but it separates the digits out in reverse order:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    	int num, n;
    	int sum = 0;
    	cout<<"Enter a number: "<<flush;
    	while (num != 0)
    		n = num % 10;
    		num = num / 10;
    		sum = sum + n;
    	cout<<"The sum is "<<sum<<endl;
    	return 0;

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Katy, Texas
    You could allocate a temporary std::string, and each time through your current loop, prepend the newer character in front of the contents of your temporary string. When done, then output your string.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Like Todd said, you need a loop, and I also think storing the numbers as a char array and using strlen() will help.
    Double Helix STL

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    When splitting up numbers, you typically get the digits in reverse order, because you don't know in advance how many you'll get so you can't really start from the back.

    The typical solution is to first create the backwards version and then reverse it.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with previously working internet code
    By abachler in forum Windows Programming
    Replies: 11
    Last Post: 04-10-2009, 04:11 AM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. MFC Multi-threading is working ... Now another problem :(
    By SyntaxBubble in forum Windows Programming
    Replies: 3
    Last Post: 11-13-2003, 08:39 PM
  4. Books on C and C++
    By kermi3 in forum A Brief History of
    Replies: 16
    Last Post: 10-28-2002, 04:18 PM
  5. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 08:03 PM