A
Amit Sharma
Hi,
I want to write a program, where until we give the value of j as
non zero, it should go on asking us values of i and j, displaying the
message "Division by zero: Floating point exception" for every input
of j as zero. Once we give the value of both i and j as non-zero, it
displays i/j and terminates.
This is the sample code i tried:
#include <signal.h>
void sig_s(int signo);
void Divide();
main( )
{
signal(SIGFPE,sig_s);
Divide();
}
void Divide()
{
int i,j,k;
printf("Enter a number\n");
scanf("%d",&i);
printf("another number\n");
scanf("%d",&j);
if( (i==0) && (j==0))
{
printf("Total is %d\n",0);
exit(0);
}
else
{
printf("Total is %d\n",i/j);
}
}
void sig_s(int signo)
{
signal(SIGFPE,sig_s);
printf("Division by zero:Floating point exception\n");
Divide();
}
It works fine as you give the denominator as zero and ask for the new
number but if you give the valid values also (e.g. i=10, j=5) Then
also it went into the Signal handler function and call the signal
handler recursively.
Could you please suggest me what wrong i am doing here ???
Thanks,
Amit
I want to write a program, where until we give the value of j as
non zero, it should go on asking us values of i and j, displaying the
message "Division by zero: Floating point exception" for every input
of j as zero. Once we give the value of both i and j as non-zero, it
displays i/j and terminates.
This is the sample code i tried:
#include <signal.h>
void sig_s(int signo);
void Divide();
main( )
{
signal(SIGFPE,sig_s);
Divide();
}
void Divide()
{
int i,j,k;
printf("Enter a number\n");
scanf("%d",&i);
printf("another number\n");
scanf("%d",&j);
if( (i==0) && (j==0))
{
printf("Total is %d\n",0);
exit(0);
}
else
{
printf("Total is %d\n",i/j);
}
}
void sig_s(int signo)
{
signal(SIGFPE,sig_s);
printf("Division by zero:Floating point exception\n");
Divide();
}
It works fine as you give the denominator as zero and ask for the new
number but if you give the valid values also (e.g. i=10, j=5) Then
also it went into the Signal handler function and call the signal
handler recursively.
Could you please suggest me what wrong i am doing here ???
Thanks,
Amit