Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 检查用户是否向int-var输入浮点值?_C++ - Fatal编程技术网

C++ 检查用户是否向int-var输入浮点值?

C++ 检查用户是否向int-var输入浮点值?,c++,C++,我有一个int-var;和使用cin,但是如何检查用户是否输入了一个浮点或给我这个问题的解决方案?将变量读入一个临时字符串,分析它是否包含,,,E,E或其他非整数字符,然后将其转换为int(如果有效) 或者,只需将值读入浮点数,并将其转换为int(根据需要进行舍入或截断)。将变量读入临时字符串,对其进行分析,查看是否包含、、E、E或其他非整数字符,然后将其转换为int(如果有效) 或者,只需将该值读入浮点并将其转换为整数(根据需要进行舍入或截断)。没有简单的方法可以做到这一点。通过>操作员输入的

我有一个int-var;和使用cin,但是如何检查用户是否输入了一个浮点或给我这个问题的解决方案?

将变量读入一个临时字符串,分析它是否包含
E
E
或其他非整数字符,然后将其转换为int(如果有效)


或者,只需将值读入浮点数,并将其转换为int(根据需要进行舍入或截断)。

将变量读入临时字符串,对其进行分析,查看是否包含
E
E
或其他非整数字符,然后将其转换为int(如果有效)


或者,只需将该值读入浮点并将其转换为整数(根据需要进行舍入或截断)。

没有简单的方法可以做到这一点。通过
>操作员输入的内容实际上并不用于与可能输入错误内容的人进行交互。您必须将输入读取为字符串,然后使用strtol之类的函数或您自己的手动代码进行检查

下面是使用strtol的方法的概要:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

bool GetInt( istream & is, int & n ) {
    string line;
    if ( ! getline( is, line ) ) {
        return false;
    }
    char * ep;
    n = strtol( line.c_str(), & ep, 10 );
    return * ep == 0;
}


int main() {
    int n;

    while(1) {
        cout << "enter an int: ";
        if ( GetInt( cin, n ) ) {
            cout << "integer" << endl;
        }
        else {
            cout << "not an integer" << endl;
        }
    }
}
#包括
#包括
#包括
使用名称空间std;
bool GetInt(istream&is,int&n){
弦线;
如果(!getline(is,line)){
返回false;
}
char*ep;
n=strtol(第10行c_str(),&ep);
返回*ep==0;
}
int main(){
int n;
而(1){

cout没有简单的方法可以做到这一点。通过
>操作符
输入的内容实际上并不用于与可能输入错误内容的人进行交互。您必须将输入作为字符串读取,然后使用strtol之类的函数或您自己的手动代码进行检查

下面是使用strtol的方法的概要:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

bool GetInt( istream & is, int & n ) {
    string line;
    if ( ! getline( is, line ) ) {
        return false;
    }
    char * ep;
    n = strtol( line.c_str(), & ep, 10 );
    return * ep == 0;
}


int main() {
    int n;

    while(1) {
        cout << "enter an int: ";
        if ( GetInt( cin, n ) ) {
            cout << "integer" << endl;
        }
        else {
            cout << "not an integer" << endl;
        }
    }
}
#包括
#包括
#包括
使用名称空间std;
bool GetInt(istream&is,int&n){
弦线;
如果(!getline(is,line)){
返回false;
}
char*ep;
n=strtol(第10行c_str(),&ep);
返回*ep==0;
}
int main(){
int n;
而(1){

cout您可以使用临时字符串和
boost::lexical_cast
将其转换为整数或浮点变量,如下所示:

#include <iostream>
#include <boost/lexical_cast.hpp>

int main() 
{ 
    using namespace std;

    string buf;
    cin >> buf;

    bool int_ok = false, float_ok = false;
    try {
        int x = boost::lexical_cast<int>( buf );
        int_ok = true;
    } catch ( boost::bad_lexical_cast e ) { int_ok = false; }
    if ( !int_ok ) {
        try {
            float x = boost::lexical_cast<float>( buf );
            float_ok = true;
        } catch ( boost::bad_lexical_cast e ) { float_ok = false; }
    }

    return 0; 
} 
#包括
#包括
int main()
{ 
使用名称空间std;
字符串buf;
cin>>buf;
bool int_ok=false,float_ok=false;
试一试{
intx=boost::词法转换(buf);
int_ok=true;
}catch(boost::bad_词法_cast e){int_ok=false;}
如果(!int_ok){
试一试{
float x=boost::词法转换(buf);
float_ok=true;
}catch(boost::bad_词法_cast e){float_ok=false;}
}
返回0;
} 

根据您想要实现的目标,可能有更好的解决方案。

您可以使用临时字符串和
boost::lexical\u cast
将其转换为整数或浮点变量,如下所示:

#include <iostream>
#include <boost/lexical_cast.hpp>

int main() 
{ 
    using namespace std;

    string buf;
    cin >> buf;

    bool int_ok = false, float_ok = false;
    try {
        int x = boost::lexical_cast<int>( buf );
        int_ok = true;
    } catch ( boost::bad_lexical_cast e ) { int_ok = false; }
    if ( !int_ok ) {
        try {
            float x = boost::lexical_cast<float>( buf );
            float_ok = true;
        } catch ( boost::bad_lexical_cast e ) { float_ok = false; }
    }

    return 0; 
} 
#包括
#包括
int main()
{ 
使用名称空间std;
字符串buf;
cin>>buf;
bool int_ok=false,float_ok=false;
试一试{
intx=boost::词法转换(buf);
int_ok=true;
}catch(boost::bad_词法_cast e){int_ok=false;}
如果(!int_ok){
试一试{
float x=boost::词法转换(buf);
float_ok=true;
}catch(boost::bad_词法_cast e){float_ok=false;}
}
返回0;
} 

根据您试图实现的目标,可能有更好的解决方案。

最简单的方法可能是接受浮点/双精度,将其转换为int,然后测试转换是否修改了值

    int i ;
    float f ;
    std::cout << "Input: " ;
    std::cin >> f ;
    i = static_cast<int>(f) ;
    if( static_cast<float>(i) != f )
    {
        std::cout << "Not an integer" ;
    }
    else
    {
        std::cout << "An integer" ;
    }
inti;
浮动f;
std::cout>f;
i=静态铸件(f);
if(静态施法(i)!=f)
{

std::cout最简单的方法可能是接受浮点/双精度,将其转换为int,然后测试转换是否修改了值

    int i ;
    float f ;
    std::cout << "Input: " ;
    std::cin >> f ;
    i = static_cast<int>(f) ;
    if( static_cast<float>(i) != f )
    {
        std::cout << "Not an integer" ;
    }
    else
    {
        std::cout << "An integer" ;
    }
inti;
浮动f;
std::cout>f;
i=静态铸件(f);
if(静态施法(i)!=f)
{

史努比,如果我是你,我就不应该那样做。你的号码仍然是浮点数。也许
浮点数a;int b;b=(int)a;
会更好。如果我是你,我就不应该那样做,史努比。你的号码仍然是浮点数。也许
浮点数a;int b;b=(int)a;
是更好的选择。是否有标准库或第三方库用于获取用户输入?是否有标准库或第三方库用于获取用户输入?