我的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
我的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;
}
相关文章:
- 代码在main()中运行,但在函数中出现错误
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 断言中的Fold表达式在某些计算机上编译,但在其他计算机上不编译
- 换位表导致测试失败(但在游戏中运行良好)
- 库标题在标题中不可见,但在 cmake build 下.cpp文件中完全可见.为什么?
- 树莓上的 Libtorch 无法加载 pt 文件,但在 ubuntu 上工作
- 在成员dynamic_bitset上使用 boost::from_block_range 时出错,但在本地dynamic
- 编译在我的 Mac 上工作,但在集群 (Linux) 上不起作用
- 我编写了代码将十进制分数转换为其二进制等效数.它编译得很好,但在执行时挂起
- 我的代码运行良好,但在游戏循环中中断
- 我的C 程序在CodeBlocks中提供了正确的结果,但在Visual Basic 2005 Express Edi
- 执行我的代码的.exe结果时错误,但在调试器中执行该代码时没有错误
- 着色器中实现双精度cos()的结果是NaN,但在CPU上运行良好.出了什么问题
- 在所有声明的双精度但整数结果中打印带有函数的圆的面积,为什么
- C++为什么我的代码在Ubuntu上给出了想要的结果,但在Windows7上却有未定义的行为
- 重载 [],但其结果在与其他运算符交互之前未解析 (C++)
- 函数不计算正确的结果,但在添加一个计数时,它会给出正确的结果
- 向量::擦除在类方法中不起作用,但在 main 中工作;没有编译器错误,只有意外的结果