4 回答

TA貢獻1876條經(jīng)驗 獲得超5個贊
根據(jù)你的問題,我的c++代碼實現(xiàn)如下:
//ps: VC++ 6.0下編譯通過
#include <iostream>
#include <limits>
using namespace std;
int main()
{
int a;
while(true )
{
cin>>a;
if(cin.good() && getchar() != '.') { //input an real number also is not allowed1: getchar() != '.'
break;
}
else { //clean the buffer
//cout<<"Error! Try agin:" ;
cin.clear();
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}
}
cout<<"a = "<<a<<endl;
return 0;
}
另外:
用typeid(var_name)可以判定任意變量的數(shù)據(jù)類型.
通過實驗?zāi)憧梢钥吹讲煌臄?shù)據(jù)類型會有對應(yīng)的一個字母表示
int--i double--d char--c bool--b 等等.
========================
例子:
#include<typeinfo>
#include<iostream>
using namespace std;
int main()
{
int a = 10;
cout<<typeid(a).name()<<endl;
double b = 1000.22;
cout<<typeid(b).name()<<endl;
return 0;
}
- 4 回答
- 0 關(guān)注
- 2191 瀏覽
添加回答
舉報