[英]Getting the error: floating point exception
I am a novice coder and I am currently working on the Usaco Training problems to improve my skills. 我是新手编码员,目前正在研究Usaco培训问题,以提高技能。 This was my code for "Greedy Gift Givers".
这是我的代码“ Greedy Gift Givers”。 When I submit the code, I get an error that says: Execution error: Your program (`gift1') exited with signal #8 (floating point exception [usually caused by accessing memory out of bounds]).
提交代码时,出现错误消息:执行错误:您的程序('gift1')退出并显示信号#8(浮点异常[通常是由于超出范围访问内存引起的)。 The program ran for 0.000 CPU seconds before the signal.
该程序在发出信号之前已运行0.000 CPU秒。 It used 3504 KB of memory.
它使用了3504 KB的内存。 " I have been looking through my code and trying to fix this error for a while now. Can anyone help? Please don't get angry with me as this is my first time posting, and like I said, I'm brand new to programming.
“我一直在仔细阅读我的代码,现在试图纠正此错误已有一段时间了。有人可以帮忙吗?请不要生我的气,因为这是我第一次发帖,就像我说的那样,我是新手编程。
/*
ID: ashton.1
PROG: gift1
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream fin ("gift1.in");
ofstream fout ("gift1.out");
int initial, account[10], recipients, NP;
string names[10], giver, receiver;
fin >> NP;
for (int k = 0; k < NP; k++)
{
fin >> names[k];
account[k] = 0;
}
for (int k = 0; k < NP; k++)
{
fin >> giver >> initial >> recipients;
for (int y = 0; y < NP; y++)
{
if( names[y] == giver)
break;
}
account[k] -= initial;
account[k] += initial % recipients;
for (int y = 0; y < recipients; y++)
{
fin >> receiver;
for (int j = 0; j < NP; j++)
{
if(names[j] == receiver)
break;
}
if(recipients != 0)
{
for(int x = 0; x < NP; x++)
{
account[x] += (int)initial / recipients;
}
}
}
fout << names[k] << " " << account[k] << endl;
}
return 0;
}
When you take initial % recipients you could be dividing by zero if there are no recipients. 当您获取初始%收件人时 ,如果没有收件人,则可以除以零。 Add a conditional statement to filter that case out;
添加条件语句以过滤掉这种情况; this is what gives the floating point exception.
这就是浮点异常的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.