下面这段代码有什么问题?为何输入正确的i和j,catch函数提还是要执行?
- #include <string>
- #include <iostream>
- #include <stdexcept>
- using namespace std;
- int main()
- {
-
- while(true)
- {
- try{
- int i,j;
- cin >> i >> j;
- //把!cin 去掉即可
- if(!cin && j != 0)
- cout << "i / j = "<< i / j <<endl;
- else
- {
- throw runtime_error("division by 0.");
- }
- }
-
- catch(runtime_error e)
- {
- char c;
- cout << "Enter y to continue or n to break:";
- cin >> c;
- if(!cin || c== 'n' || c == 'N' || c == EOF)
- break;
- else
- continue;
-
- }
-
- }
-
- return 0;
- }
解决方法:按照代码中的讲解解决