Floating Point Exception Error

Hi I am having difficulty running my program it keeps giving me a floating point exception error. Can someone give me suggestions??

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#[code]include <iostream>
using namespace std;

void int main(){
  int choice;
  float number;
 while(choice !=4){
     cout<< "Welcome to my first program! Please choose one of the menu choices below:" <<endl;
     cout<< "1 Check if a number is prime."<<endl;
     cout<< "2 Reverse display a number."<<endl;
     cout<< "3 Check if the reverse of a number is prime."<<endl;
     cout<< "4 Quit."<<endl;
     cout << "Please enter your menu choice: " << endl;
	cin >> choice;
     cout<< "Enter your number:" <<endl;
        cin >> number;
    if(number >= 0) { 

       //choice 1
	if(choice==1) {
            for(float i=1; i<= number; i++){
	        if(number% i!=0){
		   cout<< "Number is prime:" <<endl;     }
	        else {
		   cout<< "Number is NOT prime:" <<endl;  }  } }

	else if(choice == 2) {
          float new_num;
            while(number > 0){
              new_num = new_num*10 + (number % 10);
              number = number/10;
    }
              cout << new_num << endl; }

         else if(choice == 3){

    float new_number;
    while(number > 0)
    {
            new_number = new_number*10 + (number % 10);
            number = number/10;
    }
    cout << "The reversed number is: " << new_number << endl;

            for(float i=0; i<= new_number; i++){
	        if(new_number% i!=0){
		   cout<< "New number is prime:" <<endl;     }
	        else {
		   cout<< "New number is NOT prime:" <<endl;  }  } }

       else if(choice== 4){
         cout<< "Goodbye." <<endl;
         return 0;
                          }
                        }
	else  {
 		cout << "Your number is invalid because it is less than 0." << endl;
		
                      }
                     }

}[/code]
Last edited on
The % modulus operator expects integer values, but in several cases the code is using float values.

There is the fmod function in the <cmath> library (http://www.cplusplus.com/reference/cmath/fmod/), but it seems like you could just change your float variables to integers if you're going to be checking for primes.

while(choice !=4) - choice has not been initialized with a value before you try to compare it to 4 here.

If you can edit your post, highlight the code, then click the button with <> in the Format palette to the right of the post to add code tags, that would help others to read the code :)
Topic archived. No new replies allowed.