Thread: something wrong with this?

  1. #1
    Unregistered
    Guest

    something wrong with this?

    hi, I have a problem with my program. what the program does is prints a number backwards. however, the function fails for certain numbers. why is this?

    I am currently using MSVC++ 6.0

    Code:
    #ifndef _INC_IOSTREAM
    #include <iostream.h>
    #endif
    
    long int newvalue = 0; //variable to store the number backwards
    
    void PrintNumberBackwars(long int x) //recursive function
    {
    long int temp = x / 10;
    temp = x - temp * 10;
    newvalue = newvalue * 10 + temp;
    cout << temp;
    x /= 10;
    if ( x != 0 ) PrintNumberBackwards(x);
    else cout << endl;
    }
    
    int main(void) // main driver function
    {
    long int x;
    cin >> x;
    if ( cin.fail() ) return 0;
    PrintNumberBackwards(x);
    cout << newvalue;
    return 0;
    }

  2. #2
    Unregistered
    Guest
    also, which variables (if any) can I change to type reference (&) ?

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >the function fails for certain numbers.

    What numbers does it fail for?

    >which variables (if any) can I change to type reference (&) ?

    You could put newvalue in main() and pass it by reference into PrintNumberBackwards(), recursively passing by reference each time.

  4. #4
    Unregistered
    Guest
    ok, so is it better to leave the global variable alone or is it better to make it a local and pass it by reference? which one saves more memory?

    also, numbers such as 202020202020 and other big numbers don't work. why don't they? I tested the program manually by paper and everything works well. the output from my example turns out to be something like 7463847412 on output and the variable has a value of -1126087180. is it the program or is there something wrong with my algorithm?

    thanks in advance.

  5. #5
    Unregistered
    Guest
    try using a long double for all your long ints. That will make more space for your numbers. If the algorithom works right, it should clear up the problem.

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Do you have to use ints, doubles, ect? It would be a lot easier if u could just have the input be a character array. From there, you could have the function reverse the numbers in a different array by finding how long the array is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM