Thread: Floating point exception?

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Talking Floating point exception?

    Hi all!
    Can anybody see where I'm going wrong here? I think it may be something to do with the while loop in the division function but I can't work it out! Any help much appreciated...

    Code:
    // separatingDigits.cpp
    // Write program segments that accomplish each of the following:
    // a) Calculate the integer part of the quotient when integer a is divided by integer b.
    // b) Calculate the integer remainder when integer a is divided by integer b.
    // c) Use the program pieces developed in (a) and (b) to write a function that inputs an
    //    integer between 1 and 32767 and prints it as a series of digits, each pair of which is
    //    separated by two spaces. For example, the integer 4562 should print as follows:
    //    4  5  6  2
    #include <iostream>
    using namespace std;
    
    void divisionPart ( int );
    void moduloPart ( int );
    
    int main()
    {
        int userNumber = 0; // to store user input
    
        while ( userNumber < 1 || userNumber > 32767 ) // verification to ensure user input is between 1 & 32767
        {
            cout << "Enter number: "; // ask user for input
            cin >> userNumber; // save user input
        } // end while
    
        divisionPart ( userNumber ); // use functions to get the numbers
        moduloPart ( userNumber ); // to get the final number
    
    } // end main
    
    // function to calculate the integer part
    void divisionPart ( int a )
    {
        while ( a > 0 )
        {
            static int i = 10; // to keep its value each time called
            int result; // necessary so that the constant variable can be increased upon each iteration
    
            result = a / i; // find the next number
            i *= 10; // increase variable i for the next iteration
    
            cout << "  " << result; // return the result
        } // end if
    
    } // end function divisionPart
    
    // function to calculate the remainder part
    void moduloPart ( int a )
    {
        cout << "  " << a % 10;
    } // end function moduloPart

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I don't see how you could get a floating point exception, you are not using an floating point numbers.

    I would suggest that you look at this function.

    Code:
    // function to calculate the integer part
    void divisionPart ( int a )
    {
        while ( a > 0 )
        {
            static int i = 10; // to keep its value each time called
            int result; // necessary so that the constant variable can be increased upon each iteration
    
            result = a / i; // find the next number
            i *= 10; // increase variable i for the next iteration
    
            cout << "  " << result; // return the result
        } // end if
    
    } // end function divisionPart
    I don't believe that this is doing what you expect. And at some time your variable i is set to zero, giving a division by zero error.

    Jim

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The loop in divisionPart is infinite. Thus, you keep on multiplying i by 10. Due to overflow, it will as some point get weird values, and eventually will be 0. Then you get a division by zero which, on *nix, is confusingly reported as a floating point exception, even though it's an integer division by zero error.
    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

  4. #4
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51
    So I've been playing around but I still can't find the answer! Could somebody please explain to me what it is I need to do? I'm so close I can smell it!!!

    Code:
    // separatingDigits.cpp
    // Write program segments that accomplish each of the following:
    // a) Calculate the integer part of the quotient when integer a is divided by integer b.
    // b) Calculate the integer remainder when integer a is divided by integer b.
    // c) Use the program pieces developed in (a) and (b) to write a function that inputs an
    //    integer between 1 and 32767 and prints it as a series of digits, each pair of which is
    //    separated by two spaces. For example, the integer 4562 should print as follows:
    //    4  5  6  2
    #include <iostream>
    using namespace std;
    
    void divisionPart ( int );
    void moduloPart ( int );
    
    int main()
    {
        int userNumber = 0;                             // to store user input
    
        while ( userNumber < 1 || userNumber > 32767 )  // verification to ensure user input is between 1 & 32767
        {
            cout << "Enter number: ";                   // ask user for input
            cin >> userNumber;                          // save user input
        } // end while
    
        divisionPart ( userNumber );                    // use functions to get the numbers
        moduloPart ( userNumber );                      // to get the final number
    
    } // end main
    
    // function to calculate the integer part
    void divisionPart ( int a )
    {
    
        int i = 10000;                          // to keep its value each time called
        int result;                                 // necessary so that the constant variable can be increased upon each iteration
    
        while ( i > 0 )
        {
            result = ( a / i );                             // find the next number
    
            cout << result << "  ";                     // return the result
    
            i /= 10;                                    // increase variable i for the next iteration
        } // end if
    
    } // end function divisionPart
    
    // function to calculate the remainder part
    void moduloPart ( int a )
    {
        cout << a % 10 << "  ";
    } // end function moduloPart

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What exactly do you want divisionPart to do?
    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

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    So are you still getting a floating point exception? Or just observing that the program doesn't give the desired output? What you've got now is much better than what you had before!

    " the integer 4562 should print as follows: 4 5 6 2"
    Is divisionPart meant to be doing this?

    If so you need to look in the while loop again. You've got the right idea, but at the moment you're missing a step. The program will currently output
    0 4 45 456 4562

    i.e.
    4*1000
    45*100
    456*10
    ...

    For example there are 45 100s in 4562. That's correct. You need to change something in the loop to disregard the parts you've already written out. So for 100s, you only want to check 562. Which is 4562-4000, that should give you a big clue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. floating point exception?
    By snowball in forum C Programming
    Replies: 2
    Last Post: 03-04-2011, 10:45 AM
  2. Getting a floating point exception
    By SnertyStan in forum C Programming
    Replies: 13
    Last Post: 03-25-2008, 11:00 AM
  3. floating point exception
    By megastar in forum C Programming
    Replies: 6
    Last Post: 07-09-2007, 04:22 AM
  4. Floating point exception ????
    By wuzzo87 in forum C Programming
    Replies: 3
    Last Post: 04-25-2007, 02:38 AM
  5. floating point exception? what causes these?
    By salvelinus in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2002, 12:12 PM