简体   繁体   English

C ++程序不会打印命令行参数

[英]C++ program won't print command-line arguments

I have decided to get back into programming (specifically C++) and to that end I have started working on a command-line parser. 我决定重新开始编程(特别是C ++),为此,我开始研究命令行解析器。 The goal is, eventually, to turn it into a simple class that I can import in some future projects. 最终的目标是将其变成一个简单的类,我可以在将来的一些项目中将其导入。

This test program is simply supposed to list the number of arguments it received, then print them out, one per line, then exit. 只需将该测试程序列出接收到的参数的数量,然后将它们打印出来,每行一个,然后退出。

Here is my code: 这是我的代码:

#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char* argv[]){
    cout << "Es gibt " << argc << " Argumente." << endl;
    for(int i=0; i << argc; i++){
        cout << "Argument " << i << ": " << argv[i] << endl;
    }
    return 0;
}

Here is what happens in the terminal: 这是终端中发生的事情:

[user@dx4320 cmdline-parser]$ g++ cmdline-parser-test.cpp -o parsetest
[user@dx4320 cmdline-parser]$ ./parsetest eins zwei
Es gibt 3 Argumente.
[user@dx4320 cmdline-parser]$ 

Why doesn't it print the arguments as it is supposed to? 它为什么不按原样打印参数? It clearly recognizes the arguments, so what is the problem? 它清楚地认识到论点,那么问题出在哪里呢? I'm sure I am missing something obvious here. 我确定我这里缺少明显的东西。

You have the wrong operator, you need: 您有错误的运算符,您需要:

for(int i=0; i < argc; i++){
   // only one ^ is correct here

otherwise you'd have the operator << (aka "left shift") and as a result, the expression i << argc (where i is initially 0 ) always evaluates to false and the loop's body is never executed. 否则,您将拥有运算符<< (又名“左移”),结果,表达式i << argc (其中i最初为0 )始终计算为false并且永远不会执行循环的主体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM