When i compile my program i get the error "floating point exception".
I do not know why this is occurring my code. I am hoping that this is
the reason why nothing is being put into my outfile. if it is
possible could you look at my code and give me a suggestion.

Steven Layman

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int sumOfPostiveDivisor(int number)
{
   int sumofDivisor = 0;
   for (int posDivisor = 0; posDivisor <= number / 2; posDivisor ++) {
	if (number % posDivisor == 0)
	  sumofDivisor = sumofDivisor + posDivisor;
   }
return sumofDivisor;
}

bool isPerfect(int number)
{
   return sumOfPostiveDivisor(number) == number;
}

int main()
{
   const int LIMIT = 10000;

   ofstream outfile;
	
   outfile.open("perfect.out");
	
   if (outfile.fail()) {
     cerr << "Can't open perfect.out for output \n";
	return 1;
   }

   for (int number = 2; number <= LIMIT; number ++) {
     if (isPerfect(number))
	  outfile << number << " is perfect.\n";
   }

   outfile.close();
   return 0;
}
for (int posDivisor = 0; posDivisor <= number / 2; posDivisor ++) {
    if (number % posDivisor == 0)

Modulo zero is the same as division by zero.
Both are wrong.

Edited 3 Years Ago by pyTony: fixed formatting

This question has already been answered. Start a new discussion instead.