用户输入类型C++

问题描述 投票:0回答:1

嗨,我想让我的代码找出 C++ 中用户输入的变量类型

喜欢:

int x; 

cout<<"enter a vaild number: ";

cin>>x; 

if(what i should say here to check if the variable was integer or not){cout<<"correct";} 

else{cout<<" x is not vaild number";} 

我尝试过类似检查整数的方法

喜欢:

int x,check; 

cin>>x;

if(x=check){cout<<"good";}

else{cout<<"bad";} 

但是计算机总是会打印出好的语法,尽管 x 不是整数

c++ variables user-input checkout
1个回答
-1
投票
#include <typeinfo>
..
cout << typeid(x).name() << endl;

在c++中可以使用typeid来获取变量的类型

© www.soinside.com 2019 - 2024. All rights reserved.