gpt4 book ai didi

c++ - 在 C++ 中尝试/抛出/捕获 : what's wrong with my code?

转载 作者:行者123 更新时间:2023-11-28 00:53:35 24 4
gpt4 key购买 nike

请问你能检查一下这段代码吗? try/catch/throw 有什么问题?

#include<iostream>
using namespace std;

int get_input();

int main() {

int number, base_in, base_out;
bool pass = 1;

while(pass) {

double number, base_in, base_out;

try {

cout << "What's your number? ";
number = get_input();

pass = 0;

}
catch(problem_type()) {
cout << "Please, write inputs should be integer" << endl;
}

}

return 0;
}


int get_input(bool target = 1) {

double n;
cin >> n;

if(n != (int)n) throw problem_type();

if(target) {
if(n<1) throw problem_type();
}

return (int)n;

}

最佳答案

您按类型捕获。喜欢

catch(const problem_type&){ }

也就是说,如果 problem_type 是类型。我在任何地方都看不到定义......

关于c++ - 在 C++ 中尝试/抛出/捕获 : what's wrong with my code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12694504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com