>b; if()//如果条件为false,则 cout,c++,C++" /> >b; if()//如果条件为false,则 cout,c++,C++" />

如何检查C+;中变量的输入数据类型+;? 我对如何检查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 } #包括 使用名称空间std; int main() { 双a,b; couta>>b; if()//如果条件为false,则 cout

如何检查C+;中变量的输入数据类型+;? 我对如何检查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 } #包括 使用名称空间std; int main() { 双a,b; couta>>b; if()//如果条件为false,则 cout,c++,C++,如果无法将输入转换为双精度,则将为cin设置failbit。可以通过调用cin.fail()来测试 cin>>a>>b; if(cin.fail()) { cout此外,如果我记性好,下面的快捷方式应该可以工作: if (! (cin>>a>>B)) { handle error } 那个密码是绝对错误的 iostream.h不存在。请改为使用#include。其他标准标题也是如此 您需要在代码(…)中导入命名空间std。这可以通过将using namespace s

如果无法将输入转换为双精度,则将为
cin
设置
failbit
。可以通过调用
cin.fail()
来测试

cin>>a>>b;
if(cin.fail())
{ 

cout此外,如果我记性好,下面的快捷方式应该可以工作:

if (! (cin>>a>>B)) { handle error }

那个密码是绝对错误的

  • iostream.h
    不存在。请改为使用
    #include
    。其他标准标题也是如此
  • 您需要在代码(…)中导入命名空间
    std
    。这可以通过将
    using namespace std;
    放在
    main
    函数的开头来完成
  • main
    必须具有返回类型
    int
    ,而不是
    void
  • 关于您的问题,您可以通过以下代码检查读取值是否成功:

    if (!(cin >> a))
        cout << "failure." << endl;
    …
    
    if(!(cin>>a))
    
    cout(编号点现在已固定,因为它们偏离了实际问题)
    if (!(cin >> a))
        cout << "failure." << endl;
    …