如何在 Turbo c++ 中传递整数作为命令行参数
我无法通过 Turbo C++ 中的命令行传递整数值。 请帮我。
I am unable to pass integer value through the command line in turbo c++.
Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您只能将参数作为字符串传递给可执行文件。您可以使用
std::atoi
将字符串转换为整数。You can pass arguments to executable only as strings. You could use
std::atoi
to convert string to integer.您必须将其作为字符串传递,然后使用类似
atoi
或strtol
。命令行参数始终是字符串(如果您想挑剔的话,也可以是 char* :)
You'll have to pass it through as a string, and then parse it with something like
atoi
orstrtol
.Command line arguments are always strings (or
char*
s if you want to be picky :)您不能从命令行传递整数,只能传递字符串。传入您的号码,然后使用
::atoi
(或任何其他转换函数)将其转换为整数You cannot pass integers from the command line, only strings. Pass in your number, and use
::atoi
(or any other conversion function) to convert it to an integer我真的很奇怪你为什么仍然坚持使用古老的编译器!越早切换到现代编译器越好!
无论如何,执行此操作的代码如下:
I really wonder why you are still sticking to the ancient compiler! The sooner you switch to modern compilers the better it is!
Anyways the code for doing that is below:
如果您只是传递一次,并且不需要维护可以提供给 main 函数的参数,则可以转换 C++ 运行时为您的程序提供的
char*
参数通过使用 int i = atoi( argv[1] ) 。如果您要拥有更多参数,您可能也需要某种方式来命名它们。那么值得一看
getopt
函数。这允许更灵活的命令行参数。甚至还有命令行解析框架可以进行类型检查等。
If you're passing it in just for this one time, and you don't need to maintain the parameters you can give your main function, you can convert the
char*
parameters the C++ runtime provides your program with by usingint i = atoi( argv[1] )
.If you're going to have more parameters, you probably want some way to name them, too. Then it's worth taking a look at the
getopt
function. This one allows for more flexible command line parameters.There even are command-line parsing frameworks out there that allow for type-checking and the lot.