need some help

This is a discussion on need some help within the C++ Programming forums, part of the General Programming Boards category; Ok, what I'm trying to do is take an integer and display each digit, one per line, in reverse order: ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    16

    need some help

    Ok, what I'm trying to do is take an integer and display each digit, one per line, in reverse order:

    // num = 123
    //displays
    3
    2
    1

    and in normal order
    // num = 123
    1
    2
    3

    I've figured out how to do the first one with % and / but am unsure how to do this to the next one...? I have a feeling its just a minor change but its beyond me right now.

    The code I'm using for the first function is:

    Code:
    void rightToLeft(int num)
    {
    	if (num < 10)
    	{
    		cout << num << endl;
    	}
    	else
    	{
    		cout << (num % 10) << endl;
    		rightToLeft(num/10);
    	}
    }
    Any help would be greatly appreciated

    "I once saw a photograph of a large
    herd of wild elephants in Central
    Africa seeing an airplane for the first
    time, and all in a state of wild
    collective terror... As, however, there
    were no journalists among them, the
    terror died down when the airplane
    was out of sight. "


    - Bertrand Russell


  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    cin>>num;
    do
    {
    rem=num%10;
    num=num/10;
    cout<<rem<<"\n";
    }
    while(num>0);

    maybe something like this...

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    Originally posted by ihsir
    cin>>num;
    do
    {
    rem=num%10;
    num=num/10;
    cout<<rem<<"\n";
    }
    while(num>0);

    maybe something like this...
    The one I'm having trouble with it outputting it in normal order, I already figured out how to do it in reverse order. Thanks, though. (sorry if I'm not makin much sense...its a bit late...probably the reason why this is giving me so much trouble, too!)

    "I once saw a photograph of a large
    herd of wild elephants in Central
    Africa seeing an airplane for the first
    time, and all in a state of wild
    collective terror... As, however, there
    were no journalists among them, the
    terror died down when the airplane
    was out of sight. "


    - Bertrand Russell


  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    nevermind, got it:

    Code:
    	int count = 1;
    	double temp = num;
    
    	while (temp > 10)
    	{
    		temp = temp / 10;
    		count++;
    	}
    
    	while (count > 0)
    	{
    		num = temp;
    		cout << num % 10 << endl;
    		temp = temp * 10;
    		count--;
    	}
    just need to manipulate that into a recursive function (since thats what the problem is on)

    "I once saw a photograph of a large
    herd of wild elephants in Central
    Africa seeing an airplane for the first
    time, and all in a state of wild
    collective terror... As, however, there
    were no journalists among them, the
    terror died down when the airplane
    was out of sight. "


    - Bertrand Russell


  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if you put the cout statement before the recursive call it will print out the digits in reverse order. If you the call to cout after the recursive call it will print them to screen in usual order.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    Originally posted by elad
    if you put the cout statement before the recursive call it will print out the digits in reverse order. If you the call to cout after the recursive call it will print them to screen in usual order.
    *starts crying*

    I can NOT believe I didn't see that!!!! aaghhhgrhghghghhghg


    thank you for showing me the light

    "I once saw a photograph of a large
    herd of wild elephants in Central
    Africa seeing an airplane for the first
    time, and all in a state of wild
    collective terror... As, however, there
    were no journalists among them, the
    terror died down when the airplane
    was out of sight. "


    - Bertrand Russell


Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21