0

I am trying to write a code that finds perfect numbers lower than the user's input. Sample of correct output:

Enter a positive integer: 100
6 is a perfect number
28 is a perfect number
There are no more perfect numbers less than or equal to 100

But when I run my code, I get the error Floating point exception

and can not figure out why. What am I doing wrong?

Here is my code:

#include <iostream>

using namespace std;

bool isAFactor(int, int);

int main(){
    int x, y;
    int countOut, countIn;
    int userIn;
    int perfect = 0;

    cout << "Enter a positive integer: ";
    cin >> userIn;

    for(countOut = 0; countOut < userIn; countOut++){
        for(countIn = 1; countIn <= countOut; countIn++){
            if(isAFactor(countOut, countIn) == true){
                countOut = countOut + perfect;
            }
        }

        if(perfect == countOut){
            cout << perfect << " is a perfect number" << endl;
        }

        perfect++;
    }

    cout << "There are no more perfect numbers less than or equal to " << userIn << endl;

    return 0;
}


bool isAFactor(int inner, int outer){
    if(outer % inner == 0){
        return true;
    }

    else{
        return false;
    }
}
false
  • 10,264
  • 13
  • 101
  • 209
  • 7
    You are calculating x % 0. – Aki Suihkonen Apr 21 '13 at 17:42
  • 3
    It would help if you posted the real error message. I'm quite sure no compiler ever says "acception". It's also a little odd that you have floating point errors in code that is integer only. – David Heffernan Apr 21 '13 at 17:42
  • "Floating point EXception" The calculation goes x % 1. Doesn't it? It is weird that I get that error message with only int and bool values. That is why I am asking the question :P – user2304913 Apr 21 '13 at 18:03
  • 1) Yes, it does. Check your `isAFactor` function. 2) Doesn't matter. "_The SIGFPE signal is sent to a process when it executes an erroneous arithmetic operation, such as division by zero._" – awesoon Apr 21 '13 at 18:10

2 Answers2

1

The arguments are just swapped. You are calling the function as isAFactor(countOut, countIn) when you should be calling with isAFactor(countIn, countOut)

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
0

To clarify @Aki Suihkonen's comment, when performing: outer % inner If inner is zero, you will get a divide by zero error.

This can be traced backward by calling isAFactor(0, 1).
It is in your for loop in main.

The first parameter to isAFactor(countOut, countIn) is assigned in the outermost for loop: for (countOut = 0; ...

Notice the value you are initializing countOut with.

Edit 1:

Change your `isAFactor` function to:  

    if (inner == 0)
    {
       cerr << "Divide by zero.\n";
       cerr.flush();
       return 0;
    }
    if (outer % inner ...

Place a breakpoint at either cerr line above.
When the execution stops there, look at the Stack Trace. A good debugger will also allow you to examine the parameter / values at each point in the trace.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • I have changed 'bool isAFactor(int inner, int outer)' to 'isAFactor(int outer, int inner)' so it does not devide by zero anymore. But the same error message is displayed. – user2304913 Apr 21 '13 at 18:36
  • See my **Edit 1:**. Use a debugger. – Thomas Matthews Apr 21 '13 at 18:47
  • Thank you, I can see now where the problem lies. but I still don't understand why it is dividing by zero. I even tried 'if(inner == 0){ inner++;}' – user2304913 Apr 21 '13 at 19:18