/*******************************************
功能: 输入分母为0时,提示异常
作者: gnehoaix
时间: 2019\4\26
********************************************/
# include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
double v1, v2;
while (cin >> v1 >> v2)
{
try
{
if (v2 == 0)
throw std::runtime_error("第二个数不能为0!");// runtime_error为进入catch的标志
cout << v1 << " / " << v2 << " = ";
cout << (v1 / v2) << endl;
}
catch (runtime_error err)
{
// err.what()用于返回runtime_error括号中的字符
cout << err.what() << " \n是否重新?请输入:y/n" << endl;
char c;
cin >> c;
if (c != 'y' && c != 'Y') // 是&&,而不是 || 只有条件为0时,才继续循环。
break;
}
}
return 0;
}
输入分母为0时,提示异常.cpp
最新推荐文章于 2023-03-23 10:00:45 发布
