用户在float变量中存储字符串时如何抛出异常?

sha*_*hah 5 c++ exception-handling

float input;
cin>>input; // if the user type string in input then throw exception
if(!isdigit(input)){
     throw "error";
}
Run Code Online (Sandbox Code Playgroud)

但isdigit也会为数值抛出异常.

怎么解决?

Moh*_*ain 5

float input;
if (cin>>input) {
  //all is good
  ...
} else {
     throw "error";
}
Run Code Online (Sandbox Code Playgroud)

是一种方法.if如果输入以数字和else路径开头,程序将采用路径.