c++ - C++中程序显示的意外输出

标签 c++ error-handling error-correction

#include <iostream>

using namespace std;

void long_fctrl(int num[], int f_num) // Function for Factorial
   {
    --f_num; // Decrement Number to multiply with Factorial Number. 
    int i = 0, pow = 0, x, j, len = 0, k = 0;
    while(f_num > 1) // Loop for Factorial Number
    {
      i = 0;
          do { // Loop to Multiply Two Array Number
            x = (num[i]*f_num) + pow;
            num[i] = x%10;
            pow = x/10;
            ++i;
         }while((pow!=0) || (x == 0));
       --f_num;
      if(k == 0) //Condition to Find the Length of Factorial Number in array
      {
          len = i;
          k = 1;
      }
      if(i > len)// Condition for factorial array Length
        len = i;
    }
     cout << "Your Number : \n";
     for(j = len-1; j >= 0; --j) // Loop for Output of Factorial Number..
        cout << num[j];
}

int main()
{
    cout << "Enter Number (1 - 100) : ";
    int num, temp;
    cin >> num;// num is the number of which we have to calculate Factorial.
    if(num > 0 && num <= 100)
    {
        int fctrl[200] = {0};
        int i;
        temp = num;// Temp initialize to Factorial Number..
        for(i = 0; temp!= 0; ++i,temp/=10) 
        {
             fctrl[i] = temp%10;
        } // Loop to insert user entered Number in Factorial Array...
        long_fctrl(fctrl, num);  //Giving Factorial Number in Array and also
                                 //in the integer form to the Function 
    }
    return 0;
}  

程序将计算范围为1到100 ..
输入小于6的数字表示正确的输出
输入: 5
输出: 120
但是对于其他数字,它给出了错误的输出
输入: 9
输出: 8080
预期输出: 362880
我无法找到我的逻辑错误。
请,如果您能够找到任何错误,请告诉我......。
................................................... .....
................................................... ......

最佳答案

您认为结束乘法循环(while((pow!=0) || (x == 0)))是错误的。该循环需要一直运行,直到i> = len, pow!= 0。

在9的情况下,您将获得

9
72    * 8
504   * 7
524   * 6

因为循环在24之后结束,因为pow == 0和x == 2; 5是以前留下的。

关于c++ - C++中程序显示的意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34303896/

相关文章:

crc - 是否可以使用 CRC 进行基本的纠错?

c++ - 生成文件错误 : No such file or directory

c++ - 从函数返回 const 对象会阻止从外部进行 move 构造吗?

C++ 共享库显示内部符号

ruby - 如何更改 Rails 错误消息文本 "We' 抱歉,出了点问题。”

error-correction - 里德-所罗门解码

c++ - C++ 编译器如何决定何时调用 std::vector 或任何对象的移动构造函数

error-handling - _Unwind_Backtrace 用于 FreeRTOS 上的不同上下文

symfony - Symfony2 : Email error not send

Java:ECC(纠错码)库?