Floating exception (core dumped) error

Hi,
I get the error 'floating except (core dumped) at the end of this program when I run it.

EX: I input 2 for labs and input a 4 and a 6 for points. At the end of the program I expected f to compute, however once the program got out of the while loop, it gives the error mentioned above.

Could anyone explain to me why I got this error?

This error will not happen if f is not there (either I comment it out, or I delete it).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>

using namespace std;

int main () {
	int labs, points, p = 0, f = 0;
	cout << "How many labs?" << endl;
	cin >> labs;
	
	   while (labs >= 1) {
	      cout << "Enter amount of points" << endl;
	      cin >> points;
              p += points;
	      cout << p << endl;
	      labs--;
}
	      if (labs == 0)
	         f = (p*labs)/labs*10;
                 cout << f << endl;
	return 0;
}
This is calculating 0 divided by 0 and then multiplying the result by 10.
Mathematically that would be undefined.

1
2
    if (labs == 0)
        f = (p*labs)/labs*10;
Hello crowandrabbit,

To expand on what Chervil said.

Line 6, I would make all variables "doubles" or at least "floats", I tend to prefer doubles.

Line 10 along with line 15 will leave "labs" at zero when the while loop finishes making line 18 a divide by zero.

I created a new variable to use in lines 10 15 and 17 so "labs" would not be changed before the calculation on line 18. Then if "f" is to be an average of "points" I believe you are going about it the wrong way. Unless I have the wrong idea here.

Hope that helps,

Andy
Topic archived. No new replies allowed.