Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;std::使用argv的for循环中的逻辑_错误_C++ - Fatal编程技术网

C++ C++;std::使用argv的for循环中的逻辑_错误

C++ C++;std::使用argv的for循环中的逻辑_错误,c++,C++,我是C++新手,我正在尝试编写一个程序来接受命令行参数并生成一个.desktop文件。我试图实现argv值的标识,但我一直得到一个std::logic\u错误 我的代码是: #include <iostream> #include <stdlib.h> #include <stdio.h> #include <string> using namespace std; int main(int argc, char* argv[]) {

我是C++新手,我正在尝试编写一个程序来接受命令行参数并生成一个.desktop文件。我试图实现argv值的标识,但我一直得到一个std::logic\u错误

我的代码是:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>

using namespace std;

int main(int argc, char* argv[]) {

    string name;
    string comment;

    for(int i = 1; i <= argc; i++) {
        char* tmp[] = {argv[i]};
        string param = *tmp;
        string paramVal = argv[i+1];
        if(param == "-h") {
            cout << "-h        Display this help dialogue" << endl;
            cout << "-n        Set entry name" << endl;
            cout << "-c        Set entry comment" << endl;
            cout << "-e        Set entry executable path" << endl;
            cout << "-i        Set entry icon" << endl;
            break;
        }
        else if(param == "-n") {
            name = paramVal;
            i++;
            continue;
        }
        else if(param == "-c") {
            comment = paramVal;
            i++;
            continue;
        }
        else if(param == "-e") {

        }
        else if(param == "-i") {

        }
        else {
            cout << "ERROR >>> Unrecognised parameter %s" << param << endl;
        }
    }

    cout << "Name: %s\nComment: %s" << name << comment << endl;
    return(0);
}

请帮助,因为这非常令人沮丧

以下是我看到的问题:

i <= argc
这也将访问超出边界的数组


您可能想看看如何为您完成所有这些。

投票结束是一个打字错误。你犯了一个错误<代码>我喜欢他们说的,编程中有三个大问题:命名问题和一个错误。为什么要绕道
tmp
而不是
string param=argv[i]?这是试图解决问题吗?我相信
argc[argc]
没有通过数组,但它将是null ptr(作为哨兵令牌),绝对不应该取消引用。
i <= argc
string paramVal = argv[i+1];