# Thread: Floating point exception?

1. ## 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. 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. 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.

4. 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. What exactly do you want divisionPart to do?

6. 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