Little help with C++

Discussion in 'Programming' started by minhazikram, Jul 11, 2012.

  1. #1
    I am trying to take an integer input in C++ and print the input in reverse order without using any built-in functions.

    The code is pasted below


    #include <stdio.h>

    int reflect(int number, int divrange){


    int reminder=0;
    int result=0;
    int final=0;
    int a = divrange;
    while(a>=1){

    reminder = number%10;

    result = result+reminder;
    result = result*10;
    number = number/10;


    a--;



    }
    final = result/10;
    return final;

    }

    void main()
    {
    int inp=0;
    int range=0;
    printf("Enter input range\n");
    scanf("%d",&range);
    printf("Enter series of number");
    scanf("%d",&inp);

    int output = reflect(inp,range);
    printf("%d",output);


    }

    the divrange if the length of the input integer. For example : if int i = 1234; then divrange will be 4. All works well until divrange equals 8. But when i give
    the value of divrange = 9 ; and enter inp = 123456789; the value that is given as output is not satisfactory.

    Please do run the code and try it out. Hope to have some answer.

    Thankyou
     
    minhazikram, Jul 11, 2012 IP
  2. pHrEaK

    pHrEaK Active Member

    Messages:
    147
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    I'm not sure about the syntax in c++ but you could use strings and substrings. Get the number the user entered which would already be in string format then get the length of the string (.length() in most languages). You could then create an array with this length, and use a for loop to assign each position in the array a different value of the substring of the original string, then build another string variable decrementing through another for loop appending each value backwards. Then if the number had to be in integer form you could parse/cast the string value...

    I don't know how well I did explaining that but if you need anymore assistance post back up. I could possibly throw together a little example in another language if you need it. Let me know

    *edit*
    Just re-read your post and I guess your not getting the value from the user but it already exists as an integer? In this case you could parse/direct cast to string...

    There might be a completely different, much better way of doing this, but this is what first came to my mind :/
     
    Last edited: Jul 11, 2012
    pHrEaK, Jul 11, 2012 IP
  3. feelbetter

    feelbetter Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    put the all value as " " then try out that
     
    feelbetter, Jul 13, 2012 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    You got a bit too complicated for your own good methinks... I'd just loop until number was zero...

    
    int reflect(int number) {
    	int result=0;
    	while (number>0) {
    		result*=10;
    		result+=number%10;
    		number/=10;
    	}
    	return result;
    }
    
    Code (markup):
    No "range" needed, should work on any number that fits a integer type... though admittedly it doesn't handle negative numbers. (would be easy enough to special case).
     
    deathshadow, Jul 15, 2012 IP
  5. feelbetter

    feelbetter Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    sorry for the inconvenient and thanks for your reply
     
    feelbetter, Jul 15, 2012 IP