0

I am practicing some book problems, and I am not able to understand why is there a "Terminated due to signal: FLOATING POINT EXCEPTION (8)" error.

If possible, could you please explain why is there an error and how to fix it.

Thank you for your time.

I do not understand the error. Hence, I haven't tried anything

#include <iostream>

using namespace std;

const int ARR_LENGTH =11;
int main() {

    int arr[ARR_LENGTH] = {2,3,5,7,11,13,17,19,23,29};
    int input =0;
    bool isItRight; 
    bool isItPrime;


    cout <<"Input an integer between 1 and 1000" <<endl;
    while (isItRight == false){
        cin >>input;
        if (!(1000 >=input  && input >= 1))
                isItRight = false;
            else 
                isItRight =true;

        if (input<1 || input > 1000 ){
            cout <<"Input out of range" <<endl;
            cout << "enter a new value: ";
        } else if (!input){
            cout <<"wrong data type, enter a new value: ";
        }
   }

   for (int i =0; i < ARR_LENGTH; i++){
         if (input % arr[i] || input == arr[i])
             isItPrime =true;
        else 
             isItPrime =false;
   }    
   if (isItPrime == true){
        cout <<"\nThe value "<<input <<" is prime!" <<endl;
   } else if (isItPrime == false){
    int i =0;
        while (i< ARR_LENGTH){
           if ((input % arr[i]) == 0 ){
            cout <<"\nThe value "<<input <<" is divisible by: " 
                 << arr[i] <<endl;
        }
       }

   }
    return 0;
}

I expected to check prime numbers and with the strategy in place I am not able to output anything.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
Santiagopph48
  • 131
  • 1
  • 2
  • 8
  • You only define 10 elements in array of length 11, so the last element (at index 10) is equal to 0. Bad things happen when you try to divide integers by 0. – Yksisarvinen Oct 23 '19 at 15:41
  • OMG thank you, cant believe I didn't notice that! really really appreciate it! – Santiagopph48 Oct 23 '19 at 15:48
  • @Santiago `int arr[] = {2,3,5,7,11,13,17,19,23,29};` -- Then `const int ARR_LENGTH = sizeof(arr) / sizeof(arr[0]);`. – PaulMcKenzie Oct 23 '19 at 15:56
  • Also, if I didnt miss something you are using isItRight uninitialized, so, it contains random value – Boki Oct 23 '19 at 16:00

0 Answers0