floating point exception(core dumped) and can't return -1

In my book the question is "Write a function that takes two unsigned short integers and returns the result of dividing the 1st argument by the second. Do not perform the division if the second number is zero, but do return -1".

Two problems, I have managed to do it by showing an error message when the second number is zero but when I try to do it like the book wants I can't return -1 and I get a "floating point exception (core dumped)" message. Please help.

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
#include<iostream>

using namespace std;

typedef unsigned short int USHORT;

USHORT div (USHORT dividend, USHORT divisor);

int main(){

USHORT aDividend = 0;
USHORT aDivisor = 0;
float theAnswer = 0;

cout << "Enter the dividend.\n";
cin >> aDividend;
cout << "Enter the divisor.\n";
cin >> aDivisor;

theAnswer = div(aDividend,aDivisor);

if (aDividend == 0){
cout << theAnswer <<"Not possible....\n";}
else{cout << "The answer is : "  << theAnswer << endl;}

}

USHORT div(USHORT thedivisor, USHORT thedividend)
{

if(thedivisor == 0){
return -1;}
else{
return (thedivisor / thedividend);}

}
Last edited on
Do not perform the division if the second number is zero

Let me rename the variables in your function:
1
2
3
4
5
6
7
8
9
USHORT div( USHORT first, USHORT second )
{
  if ( first == 0) { // this is what you wrote. Does it look right?
    return -1;
  }
  else {
    return ( first / second );
  }
}



Second, does anything in the question say that the result has to be an USHORT?
no it doesn't say anything about the results variable type.
this is what ive got now and I got rid of the floating point wrror even with a USHORT as the answer but I still can't get the -1 in to the answer.

include<iostream>

using namespace std;

typedef unsigned short int USHORT;

float div (USHORT dividend, USHORT divisor);

int main(){

USHORT aDividend = 0;
USHORT aDivisor = 0;
USHORT theAnswer = 0;

cout << "Enter the dividend.\n";
cin >> aDividend;
cout << "Enter the divisor.\n";
cin >> aDivisor;

theAnswer = div(aDividend,aDivisor);

if (aDivisor == 0){
cout << theAnswer <<" Not possible....\n";}
else{cout << "The answer is : " << theAnswer << endl;}

}

float div(USHORT thedividend, USHORT thedivisor)
{

if(thedivisor == 0){
return -1;}
else{
return (thedividend / thedivisor);}

Last edited on
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
#include <iostream>

// quotient of integer division:
// an unsigned integer initialised with a value of -1
// will be interpreted as a positive value. so make the return type
// a signed integer type which can hold (without loss of information)
// all the possible values that an unsigned short can hold.
// we assumes that that type is int (in practice, this is always true)
// returns -1 if the dividend is zero
int iquotient( unsigned short divisor, unsigned short dividend ) ;

// floating point division
// returns -1 (instead of +inf) if the dividend is zero
double divide( unsigned short divisor, unsigned short dividend ) ;

int main()
{
    std::cout << iquotient( 270, 25 ) << ' ' << divide( 270, 25 ) << '\n' // 10 10.8
              << iquotient( 260, 0 ) << ' ' << divide( 270, 0 ) << '\n' ; // -1 -1
}

int iquotient( unsigned short divisor, unsigned short dividend )
{
    return dividend == 0 ? -1 : divisor/dividend ;
}

double divide( unsigned short divisor, unsigned short dividend )
{
    return dividend == 0 ? -1 : double(divisor)/dividend ;
}

http://coliru.stacked-crooked.com/a/af87348c7d772608
thanks I think what the teacher wanted was:

#include<iostream>

using namespace std;

typedef signed short int SHORT;
typedef unsigned short int USHORT;

SHORT div (USHORT dividend, USHORT divisor);

int main(){

USHORT aDividend = 0;
USHORT aDivisor = 0;
SHORT theAnswer = 0;

cout << "Enter the dividend.\n";
cin >> aDividend;
cout << "Enter the divisor.\n";
cin >> aDivisor;

theAnswer = div(aDividend,aDivisor);

if (aDivisor == 0){
cout << theAnswer <<" Not possible....\n";}
else{cout << "The answer is : " << theAnswer << endl;}

}

SHORT div(USHORT thedividend, USHORT thedivisor)
{

if(thedivisor == 0){
return -1;}
else{
return (thedividend / thedivisor);}

}

Your help is greatly appreciated :-)
Topic archived. No new replies allowed.