사용자 입력보다 완전한 숫자를 찾는 코드를 작성하려고합니다. 올바른 출력의 샘플 :"부동 소수점 예외"오류가 발생하는 이유는 무엇입니까?
이하 더 이상 완벽한 숫자가 있습니다 완벽한 수은 양의 정수 입력 : 100
6은 100
완벽한 수
(28)입니다
하지만 코드를 실행하면 오류가 발생합니다. Floating point exception
이유를 알 수 없습니다. 내가 도대체 뭘 잘못하고있는 겁니까? 인수는 단지 교환하는
#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;
}
}
x % 0을 계산 중입니다. –
실제 오류 메시지를 게시하면 도움이됩니다. 나는 어떤 컴파일러도 "acception"을 언급하지 않는다고 확신한다. 또한 정수만있는 코드에서 부동 소수점 오류가 발생하는 것은 다소 이상합니다. –
"부동 소수점 예외" 계산이 x % 1로 진행됩니다. int 및 bool 값만 사용하여 오류 메시지가 표시되는 것은 이상한 일입니다. 그것이 내가 질문을하는 이유입니다. P – user2304913