Я хочу использовать аргумент в командной строке как целое число. Я также хочу использовать блок 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
}
strtol
если вы не хотите использовать исключения.
Еще один фрагмент, который может помочь, используя 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;
}