[英]Passing values to main(int, char**)
I have a program that displays ascii values and letter, but now I have modify the program to display only the Spanish characters I am looking for. 我有一个显示ascii值和字母的程序,但是现在我修改了该程序以仅显示我要查找的西班牙语字符。 The program is called, yes, you got it, "ascii".
该程序称为,是的,您知道它为“ ascii”。 What the user has to do is type
C:>ascii
or C:>ascii all
to display all the ascii characters. 用户要做的是键入
C:>ascii
或C:>ascii all
来显示所有ascii字符。 To display only the Spanish characters the user must type C:>ascii es
. 要仅显示西班牙语字符,用户必须输入
C:>ascii es
。
For this I have written this simple program 为此,我编写了这个简单的程序
int main(int argc, char** argv) {
if(argv[0] == "es"){
abc::ascii_es();
}
if(argv[0] == "all"){
abc::ascii_all();
}
else
abc::ascii_es();
return 0;
}
but the program always jumps to the ascii_es
method in the else condition keyword. 但是程序总是在else条件关键字中跳转到
ascii_es
方法。 What am I doing wrong? 我究竟做错了什么?
std::string
or by using strcmp(...) == 0
. std::string
或使用strcmp(...) == 0
来解决此问题。 argv[0]
is the name of your program. argv[0]
是您的程序的名称。 Use argv[1]
for the first argument. argv[1]
。 argv[1]
will cause undefined behavior. argv[1]
将导致未定义的行为。 #include <string>
int main(int argc, char** argv) {
if (argc < 2)
abc::ascii_es();
else if (argv[1] == std::string("es"))
abc::ascii_es();
else if (argv[1] == std::string("all"))
abc::ascii_all();
else
abc::ascii_es();
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.