Simple program Windows error

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 n, NminusK_F, temp, total;
	cout << "Enter n: "; cin >> n;
	total = 0;
	for(int k = 0; k <= n; k++)
	{
		NminusK_F = 1; temp = n-k;
		for(int i = 0; i < (n-k); i++)
		{
			NminusK_F *= temp;
			temp = (n-k)-1;
		}
		total += NminusK_F/k;
	}
	cout << "Fn+1 = " << total;
	return 0;
}


Anything wrong in this code causing Windows to give me "Windows program stopped working Finding solution etc. " after I enter a number.....
Last edited on
Probably a problem with integer division;
try making everything into a double data type and see what happens.
gives me inf which is wrong.
Step through it with a debugger. That will tell you where it crashes, and let you see the state of the memory at the point where it crashes.

Edit: Having said that, it's obvious why it's crashing. What will happen the first time line 17 executes?
Last edited on
You try to divide with zero!!
k=0 at the beginning of the loop.

 
total += NminusK_F/k; //k is zero 
Last edited on
Okay got it, your all right. Thanks much.
Topic archived. No new replies allowed.