我对如何在C++中检查输入变量的数据类型有一个疑问.
#include<iostream>
using namespace std;
int main()
{
double a,b;
cout<<"Enter two double values";
cin>>a>>b;
if() //if condition false then
cout<<"data entered is not of double type";
//I'm having trouble for identifying whether data
//is double or not how to check please help me
}
Run Code Online (Sandbox Code Playgroud)
如果输入无法转换为double,failbit则将设置为cin.这可以通过调用来测试cin.fail().
cin>>a>>b;
if(cin.fail())
{
cout<<"data entered is not of double type";
}
Run Code Online (Sandbox Code Playgroud)
更新:正如其他人指出的那样,您也可以使用!cin而不是cin.fail().两者是等价的.
| 归档时间: |
|
| 查看次数: |
6550 次 |
| 最近记录: |