命令行参数 - int而不是ascii

时间:2013-12-10 22:01:47

标签: c++ command-line-arguments

我正在编写一个C ++控制台应用程序,我无法弄清楚如何将命令行参数解释为数字而不是ascii

int id = *argv[2];
if (id != 0)    //really, if a user enters "0", then id = 48 (0x30), not 0
{
    cout << "unknown product ID" << endl; endl;
    cout << "hit return to exit" << endl;
}

这种事情通常怎么做? 感谢

1 个答案:

答案 0 :(得分:3)

您必须将字符串转换为整数。

int id = atoi(argv[2]);
// Or id = std::stoi(argv[2]), if you have C++11-standard compiler
// Or id = boost::lexical_cast<int>(argv[2]) if you have boost