integer division by zero exception?
Feb 26, 2014 at 4:34am UTC
the program can compile successfully but when i input it prompts"exception integer division by zero"
why? thank u. this is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
using namespace std;
int main()
{
int number;
cin>>number;
int m;
int friends[number];
for (int i=0;i<number;i++)
{
friends[i]=i+1;
}
for (int i=0;i<=m;i++)
{
for (int p=0;p<=number;p++)
{
if ( friends[p]%i!=0)
{
friends[i]=0;
}
}
}
for (int i=0;i<number;i++)
{
if (friends[i]!=0)
{
cout<<friends[i]<<endl;
}
}
}
Feb 26, 2014 at 4:48am UTC
1 2 3 4 5
for (int i=0;i<=m;i++) // i = 0
{
for (int p=0;p<=number;p++) // i is still 0
{
if ( friends[p]%i!=0) // what's the remainder of division by i?
Feb 26, 2014 at 7:39am UTC
thank you very much but
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
// ConsoleApplication28.cpp :
//
#include <iostream>
using namespace std;
int main()
{
int number;
cin>>number;
int m;
int friends[number]={};
for (int i=0;i<number;i++)
{
friends[i]=i+1;
}
for (int i=1;i<=m+1;i++)
{
for (int p=0;p<=number;p++)
{
if ( friends[p]%i!=0)
{
friends[i]=0;
}
}
}
for (int i=0;i<number;i++)
{
if (friends[i]!=0)
{
cout<<friends[i]<<endl;
}
}
}
why cant i compile this?thank you
Last edited on Feb 26, 2014 at 7:39am UTC
Feb 26, 2014 at 7:54am UTC
When a compiler refuses to compile something, it usually does tell a reason. An error message. The best long term solution is to learn to interpret the error messages, but for now you should show us the error message.
Topic archived. No new replies allowed.