gpt4 book ai didi

c++ - 命令行参数 - int 而不是 ascii

转载 作者:太空狗 更新时间:2023-10-29 21:22:29 25 4
gpt4 key购买 nike

我正在编写一个 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;
}

这种事情通常是怎么做的?谢谢

最佳答案

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

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

关于c++ - 命令行参数 - int 而不是 ascii,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20506249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com