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

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    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:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	int num, n;
    	int sum = 0;
    	cout<<"Enter a number: "<<flush;
    	cin>>num;
    
    	while (num != 0)
    	{
    		n = num % 10;
    		num = num / 10;
    		sum = sum + n;
    		cout<<setw(2)<<n;
    	} 
    	cout<<endl;
    	cout<<"The sum is "<<sum<<endl;
    	return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    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
    Posts
    8,895
    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!
    CornedBee

    "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 Cprogramming.com
    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