C++ 将参数转换为整数,并进行错误检查

C++ 将参数转换为整数,并进行错误检查,c++,integer,command-line-arguments,C++,Integer,Command Line Arguments,我想在命令行中将参数用作整数。我还想使用try/catch块检查输入是否正确: int i; try{ i=atoi(argv[1]); }catch(int e){ printf("error: need integer\n"); return 0; } 但atoi似乎接受其他输入,如字符和符号。我怎样才能克服这个问题 谢谢, dalvo使用stoi: try { i = std::stoi("1234"); } catch (std::invalid_a

我想在命令行中将参数用作整数。我还想使用try/catch块检查输入是否正确:

int i;
try{
    i=atoi(argv[1]);
}catch(int e){
    printf("error: need integer\n");
    return 0;
}
但atoi似乎接受其他输入,如字符和符号。我怎样才能克服这个问题

谢谢, dalvo

使用
stoi

try {
  i = std::stoi("1234");
}    
catch (std::invalid_argument) {
  //error
}
使用
stoi

try {
  i = std::stoi("1234");
}    
catch (std::invalid_argument) {
  //error
}
使用
stoi

try {
  i = std::stoi("1234");
}    
catch (std::invalid_argument) {
  //error
}
使用
stoi

try {
  i = std::stoi("1234");
}    
catch (std::invalid_argument) {
  //error
}

只需使用
std::stringstream
再添加一个可能有用的代码片段

#include<iostream>
#include<sstream>
#include<algorithm>
#include<locale>

int main(int argc, char** argv){

    // it should be a double, but makes easier 
    // to show ideas above
    int i;

    std::stringstream sstr(argv[1]);

    bool is_all_digit = true;
    // i tried to use a std::all_of, but get stuck...
    // this for do the same job
    // keep in mind thar for double, exponencial, etc
    //   there should be more valid chars
    // If you use only sstr >> i, if argv is, for instance 
    //  '12345asdfg' it will consider 12345.
    for(char& c: sstr.str()){
        is_all_digit &= std::isdigit(c);  
    }

    if( is_all_digit && !(sstr >> i).fail() )
        std::cout << "i == " << i << std::endl;
    else
        std::cerr << "Could not convert " << argv[1] << " to 'int'" << std::endl;
    return 0;
}
#包括
#包括
#包括
#包括
int main(int argc,字符**argv){
//应该是双人的,但这样更容易
//展示上面的想法
int i;
std::stringstream sstr(argv[1]);
布尔值为所有数字=真;
//我试着用std::all_of,但被卡住了。。。
//这是为了做同样的工作
//记住双倍、指数等
//应该有更多的有效字符
//例如,如果argv为,则仅使用sstr>>i
//'12345 ASDFG’将考虑12345。
for(char&c:sstr.str()){
是所有数字&=std::isdigit(c);
}
如果(是所有数字(&!)(sstr>>i).fail())

std::cout使用std::stringstream
只需再添加一个可能有用的代码片段

#include<iostream>
#include<sstream>
#include<algorithm>
#include<locale>

int main(int argc, char** argv){

    // it should be a double, but makes easier 
    // to show ideas above
    int i;

    std::stringstream sstr(argv[1]);

    bool is_all_digit = true;
    // i tried to use a std::all_of, but get stuck...
    // this for do the same job
    // keep in mind thar for double, exponencial, etc
    //   there should be more valid chars
    // If you use only sstr >> i, if argv is, for instance 
    //  '12345asdfg' it will consider 12345.
    for(char& c: sstr.str()){
        is_all_digit &= std::isdigit(c);  
    }

    if( is_all_digit && !(sstr >> i).fail() )
        std::cout << "i == " << i << std::endl;
    else
        std::cerr << "Could not convert " << argv[1] << " to 'int'" << std::endl;
    return 0;
}
#包括
#包括
#包括
#包括
int main(int argc,字符**argv){
//应该是双人的,但这样更容易
//展示上面的想法
int i;
std::stringstream sstr(argv[1]);
布尔值为所有数字=真;
//我试着用std::all_of,但被卡住了。。。
//这是为了做同样的工作
//记住双倍、指数等
//应该有更多的有效字符
//例如,如果argv为,则仅使用sstr>>i
//'12345 ASDFG’将考虑12345。
for(char&c:sstr.str()){
是所有数字&=std::isdigit(c);
}
如果(是所有数字(&!)(sstr>>i).fail())

std::cout使用std::stringstream
只需再添加一个可能有用的代码片段

#include<iostream>
#include<sstream>
#include<algorithm>
#include<locale>

int main(int argc, char** argv){

    // it should be a double, but makes easier 
    // to show ideas above
    int i;

    std::stringstream sstr(argv[1]);

    bool is_all_digit = true;
    // i tried to use a std::all_of, but get stuck...
    // this for do the same job
    // keep in mind thar for double, exponencial, etc
    //   there should be more valid chars
    // If you use only sstr >> i, if argv is, for instance 
    //  '12345asdfg' it will consider 12345.
    for(char& c: sstr.str()){
        is_all_digit &= std::isdigit(c);  
    }

    if( is_all_digit && !(sstr >> i).fail() )
        std::cout << "i == " << i << std::endl;
    else
        std::cerr << "Could not convert " << argv[1] << " to 'int'" << std::endl;
    return 0;
}
#包括
#包括
#包括
#包括
int main(int argc,字符**argv){
//应该是双人的,但这样更容易
//展示上面的想法
int i;
std::stringstream sstr(argv[1]);
布尔值为所有数字=真;
//我试着用std::all_of,但被卡住了。。。
//这是为了做同样的工作
//记住双倍、指数等
//应该有更多的有效字符
//例如,如果argv为,则仅使用sstr>>i
//'12345 ASDFG’将考虑12345。
for(char&c:sstr.str()){
是所有数字&=std::isdigit(c);
}
如果(是所有数字(&!)(sstr>>i).fail())

std::cout使用std::stringstream
只需再添加一个可能有用的代码片段

#include<iostream>
#include<sstream>
#include<algorithm>
#include<locale>

int main(int argc, char** argv){

    // it should be a double, but makes easier 
    // to show ideas above
    int i;

    std::stringstream sstr(argv[1]);

    bool is_all_digit = true;
    // i tried to use a std::all_of, but get stuck...
    // this for do the same job
    // keep in mind thar for double, exponencial, etc
    //   there should be more valid chars
    // If you use only sstr >> i, if argv is, for instance 
    //  '12345asdfg' it will consider 12345.
    for(char& c: sstr.str()){
        is_all_digit &= std::isdigit(c);  
    }

    if( is_all_digit && !(sstr >> i).fail() )
        std::cout << "i == " << i << std::endl;
    else
        std::cerr << "Could not convert " << argv[1] << " to 'int'" << std::endl;
    return 0;
}
#包括
#包括
#包括
#包括
int main(int argc,字符**argv){
//应该是双人的,但这样更容易
//展示上面的想法
int i;
std::stringstream sstr(argv[1]);
布尔值为所有数字=真;
//我试着用std::all_of,但被卡住了。。。
//这是为了做同样的工作
//记住双倍、指数等
//应该有更多的有效字符
//例如,如果argv为,则仅使用sstr>>i
//'12345 ASDFG’将考虑12345。
for(char&c:sstr.str()){
是所有数字&=std::isdigit(c);
}
如果(是所有数字(&!)(sstr>>i).fail())


std::cout check out this thank for the article,我不知道有许多不同的函数,它们更稳定/高效等。check out this thank for the article,我不知道有许多不同的函数,它们更稳定/高效等。check out this thank for the article,我不知道有许多不同的函数离子更稳定,效率更高。检查这篇文章的感谢,不知道有许多不同的功能,更稳定/效率等等。或者,C++,Strutol <代码>如果你不想使用例外。谢谢,这对我来说是有效的:这是跛脚的,非常跛的。我认为这个问题是关于C++的。@保罗。你是什么意思?他的答案是C++。简单地说,这是最好的时间(事实上情况更糟)。你根本就不使用C++,更不用说它能让你做一些简单的事情了。或者“代码> StrutoL/CUT>如果你不想使用异常。谢谢,这对我来说是有效的。”这是跛脚的,非常跛。我认为这个问题是关于C++的。@保罗。你是什么意思?这是一个C++答案。简单的,这就是OLTE C。(事实上,情况更糟)你根本就不使用C++,更不用说它能让你做一些简单的事情了。或者“代码> StrutoL/CUT>如果你不想使用异常。谢谢,这对我来说是有效的。”这是跛脚的,非常跛。我认为这个问题是关于C++的。@保罗。你是什么意思?这是一个C++答案。简单的,这就是OLTE C。(事实上,情况更糟)你根本就不使用C++,更不用说它能让你做一些简单的事情了。或者“代码> StrutoL/CUT>如果你不想使用异常。谢谢,这对我来说是有效的。”这是跛脚的,非常跛。我认为这个问题是关于C++的。@保罗。你是什么意思?这是一个C++答案。简单的,这就是OLTE C。(事实上情况更糟)。这里根本不使用C++,更不用说它能让你做一些简单的事情了。