我的C 程序在CodeBlocks中提供了正确的结果,但在Visual Basic 2005 Express Edi

My c++ program gives correct results in codeblocks but gives incorrect results in visual basic 2005 express edition

本文关键字:但在 结果 Visual Basic Edi Express 2005 程序 CodeBlocks 我的      更新时间:2023-10-16

我的C 程序在CodeBlocks中给出了正确的结果,但在Visual Basic 2005 Express Edition中给出了错误的结果。任何人都可以指导我我做错了什么:)谢谢:)这是我使用函数查找阶乘的程序。

#include <iostream>
using namespace std;
int fact( int a)
{
    if (a>1)
    return a*fact(a-1);
}
int  main()
{
    cout<<"Enter a number to find its factorial : ";
    int a;
    cin>>a;
    cout<<"Factorial of "<<a<<" is "<<fact(a)<<endl<<endl;
}

导致CodeBlocks

Enter a number to find its factorial : 5
Factorial of 5 is 120

导致Visual Basic 2005 Express Edition

Enter a number to find its factorial : 5
Factorial of 5 is -96

您的代码行为不确定。

如果fact函数中的a <= 1,您未能返回值。未能返回值导致不确定的行为,因此您看到的不同结果。

校正应为:

int fact( int a)
{
    if (a>1)
       return a*fact(a-1);
    return 1; 
}
相关文章: