C++ 的整数参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4951952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
integer arguments for c++
提问by Rob
I have a sort of calculator in C++ that should accept arguments when executed. However, when I enter 7 as an argument, it might come out to be 10354 when put into a variable. Here is my code:
我有一种 C++ 计算器,它在执行时应该接受参数。但是,当我输入 7 作为参数时,放入变量时可能会显示为 10354。这是我的代码:
#include "stdafx.h"
#include <iostream>
int main(int argc, int argv[])
{
using namespace std;
int a;
int b;
if(argc==3){
a=argv[1];
b=argv[2];
}
else{
cout << "Please enter a number:";
cin >> a;
cout << "Please enter another number:";
cin >> b;
}
cout << "Addition:" << a+b << endl;
cout << "Subtaction:" << a-b << endl;
cout << "Multiplycation:" << a*b << endl;
cout << "Division:" << static_cast<long double>(a)/b << endl;
system("pause");
return 0;
}
回答by Ben Voigt
Wherever did you get int argv[]
? The second argument to main
is char* argv[]
.
你从哪里得到的int argv[]
?的第二个参数main
是char* argv[]
。
You can convert these command line arguments from string to integer using strtol
or to floating-point using strtod
.
您可以使用 将这些命令行参数从字符串转换为整数,strtol
或使用 转换为浮点数strtod
。
For example:
例如:
a=strtol(argv[1], nullptr, 0);
b=strtol(argv[2], nullptr, 0);
But you can't just change the parameter type, because the operating system is going to give you your command-line arguments in string form whether you like it or not.
但是您不能只更改参数类型,因为无论您喜欢与否,操作系统都会以字符串形式为您提供命令行参数。
NOTE: You must #include <stdlib.h>
(or #include <cstdlib>
and using std::strtol;
) to use the strtol
function.
注意:您必须#include <stdlib.h>
(或#include <cstdlib>
和using std::strtol;
)才能使用该strtol
功能。
If you want error-checking, use strtol
instead of atoi
. Using it is almost as easy, and it also gives you a pointer to the location in the string where parsing terminated. If that points to the terminating NUL, parsing was successful. And of course it is good that you verify argc
to make sure the user provided enough parameters, and avoid trying to read missing parameters from argv
.
如果您想进行错误检查,请使用strtol
代替atoi
。使用它几乎同样简单,它还为您提供了一个指向字符串中解析终止位置的指针。如果这指向终止的 NUL,则解析成功。当然,您最好进行验证argc
以确保用户提供了足够的参数,并避免尝试从argv
.
Example of error checking:
错误检查示例:
char* endp;
a = strtol(argv[1], &endp, 0);
if (endp == argv[1] || *endp) { /* failed, handle error */ }
回答by Jonathan Wood
The function signature is int main(int argc, char *argv[])
. argv is an array of string pointers.
函数签名是int main(int argc, char *argv[])
. argv 是一个字符串指针数组。
If the argument is 7, it will be in the form of a string ("7"). Use atoi()
to convert it to the number 7.
如果参数为 7,它将采用字符串的形式(“7”)。使用atoi()
将其转换为数字7。
回答by Mahesh
Second argument in the main should either either be char* argv[]
or char** argv
. Then you have to have convert them to int
.
main 中的第二个参数应该是char* argv[]
或char** argv
。然后你必须将它们转换为int
.