c++ 容错问题

来源:百度知道 编辑:UC知道 时间:2024/05/23 15:41:06
我想在输入一些整数两个一组求和,当0 0时结束,而输入小数或其他时时显示“出错”

1 2
1.1 2
0 0
输出
3
出错
求高人解答我查了下,好像用try什么的,谢谢了

#include<iostream>
using namespace std;

void main()
{

int a,b;
do
{
if(scanf("%d%d",&a,&b)==2)
{
if(a!=0||b!=0)
cout<<a+b<<endl;
else break;
}
else
{
fflush(stdin);
cout<<"error"<<endl;

}
}while(1);

}
初步能实现 还有一些问题没解决

#include <iostream>

using namespace std;
int main()
{
char *buf;
try
{
buf = new char[512];
if( buf == 0 )
throw "Memory allocation failure!";
}
catch( char * str )
{
cout << "Exception raised: " << str << '\n';
}
// ...
return 0;
}
这个是MSDN里面的,我直接给你复制过来了,好好分析分析吧

哦 是异常的处理吧 求和的异常?