为什么此应用程序的代码表示该应用程序已停止工作?

时间:2019-04-24 13:31:45

标签: c++

如何估算此问题的答案:

e = 1+ 1/1! + 1/2! + 1/3!+...

我知道我的解决方案不正确,但是什么是正确的解决方案?为什么在编译时却没有给出错误提示,但是却表示该应用已停止工作

#include <iostream>

using namespace std;

int main()
{

    int x;

    cout <<"Enter the accuracy of the expression "
         <<"1+1/1!+1/2!+1/3!+... :";

    cin >> x;

    int factorial;

    int counter = 1;

    int x2;

    int e = 0;

    x2 = x;

    factorial = x;

    while(x2 > 1)
    {
        while(counter <=x2 && x>0)
        {
            factorial*=(x-1);

            x = x-1;
            counter++;
        }
        factorial = 1/factorial;
        e+=factorial;
        x2--;
        x = x2;
        factorial = x2;
    }
    e+=1;
    cout <<"The answer is "<<e;
}

0 个答案:

没有答案