4 回答

TA貢獻1876條經驗 獲得超5個贊
根據你的問題,我的c++代碼實現如下:
//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)可以判定任意變量的數據類型.
通過實驗你可以看到不同的數據類型會有對應的一個字母表示
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 關注
- 2193 瀏覽
添加回答
舉報