关于一个用C++编的加法计算器的问题.......

来源:百度知道 编辑:UC知道 时间:2024/05/17 05:48:32
刚刚用c++编了一个加法计算器。当我计算位数较低的加法,入8加9,或520加63,都能算出来。
可是,当我算1394854325307加49471928347194 时,连答案还没有输出,程序就关闭了!!!震惊啊!!!!请问哪位达人知道这是怎么回事啊??代码如下:

#include<iostream>
int Add(int first,int second)
{
std::cout<<"In Add(),received"<<first<<"and"<<second<<"\n";
return (first+second);
}

int main()
{
using std::cout;
using std::cin;

cout<<"I'm in main()!\n";
int a,b,c;
cout<<"Enter two numbers: ";
cin>>a;
cin>>b;
cout<<"\nCalling Add()\n";
c=Add(a,b);
cout<<"\nBack in main().\n";
cout<<"c was set to"<<c;
cout<<"\nExiting...\n\n";
char response;
cin>>response;
return 0;
}

溢出。
把int换成double看看

因为你用的是int型,C++里面整型int的取值范围为-32768 - 32767
所以在你算1394854325307加49471928347194 时,直接就溢出崩掉了

int Add(int first,int second)
应改为
long int Add(int first,int second)
就能用了