为什么我会收到错误"Floating point exception"?
Why do I get the error "Floating point exception"?
我试图写一个代码,找到完美的数字低于用户的输入。正确输出示例:
输入正整数:100
6是一个完全数
28是一个完全数
再没有小于等于100的完全数
但是当我运行我的代码时,我得到错误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;
}
}
只是交换了参数。当您应该调用isAFactor(countIn, countOut)
时,您正在调用isAFactor(countOut, countIn)
函数
澄清@Aki Suihkonen的评论,在表演时:outer % inner
如果inner
为零,您将得到一个除以零错误。
这可以通过调用isAFactor(0, 1)
来反向跟踪。它在main
的for
循环中。
isAFactor(countOut, countIn)
的第一个形参在最外层的for
循环中赋值:for (countOut = 0; ...
注意您初始化countOut
时使用的值。
编辑1:
Change your `isAFactor` function to:
if (inner == 0)
{
cerr << "Divide by zero.n";
cerr.flush();
return 0;
}
if (outer % inner ...
在cerr
以上任意一行设置一个断点。
当执行停止时,查看堆栈跟踪。一个好的调试器还允许您在跟踪的每个点检查参数/值。
相关文章:
- Qt5 部署"procedure entry point could not be located"
- 我想获取点的属性,它报告错误 C3867:"point::output_x":非标准语法;使用"&"创建指向成员的指针
- 如何从内存中分配GDI+ POINT类地址?
- 如何在OpenCV中存储部分轮廓点喜欢新矢量中的左侧,该类型为<vector<vector<Point>>
- 在每个向量<向量中查找最小/最大 x,y<Point>>
- "Entry Point Not Found" Azure 中的错误 LightGBM R 包
- 如何修复'The procedure entry point SDL_RWclose could not be located in the dynamic link library'
- 如何创建geometry_msgs :: point
- 为什么我会得到"floating point exception"
- Paraview:Live Point Cloud可视化插件
- Libavcodec "the procedure entry point for av_frame_alloc could not be located" Visual Studio 2017 C+
- 无法运行Qt C++发布版本。 'The procedure entry point could not be located'
- 没有用于初始化'point'的匹配构造函数
- CGAL 如何将 std::vector<Point> 点保存到 .xyz 文件中?
- 我如何理解"Point of Instantiation"的含义
- "[Error] pointer value used where a floating point value was expected" 如何解决此错误?
- C++ "Floating Point Enum"
- "Floating-point invalid operation"将浮点数输入到字符串流时
- C++ Floating-Point van Emde Boas (vEB) Priority Queue
- 为什么我会收到错误"Floating point exception"?