当用户将字符串存储在 float 变量中时如何抛出异常?
how throw exception when user store string in float variable?
float input;
cin>>input; // if the user type string in input then throw exception
if(!isdigit(input)){
throw "error";
}
但是 isdigit 也会为数值抛出异常。
如何解决?
float input;
if (cin>>input) {
//all is good
...
} else {
throw "error";
}
是一种方法。如果输入以数字开头,程序将采用 if
路径,否则采用 else
路径。
float input;
cin>>input; // if the user type string in input then throw exception
if(!isdigit(input)){
throw "error";
}
但是 isdigit 也会为数值抛出异常。
如何解决?
float input;
if (cin>>input) {
//all is good
...
} else {
throw "error";
}
是一种方法。如果输入以数字开头,程序将采用 if
路径,否则采用 else
路径。