检查命令行输入以查找C ++中的错误

时间:2013-11-23 13:51:49

标签: c++ command-line-arguments

我有一个这个程序刚刚工作,但改变了一些东西,现在我的错误处理已经疯了。我几乎碰到了我的头靠在墙上,试图把它放回去,但不管我现在做什么,它都会出错。

该程序应该采用命令行参数来定义行和列,并基于此创建动态2D数组。格式为“-rows(number)-columns(number)”。在我完成任务之前,我尝试添加一些案例,但我必须在其他地方改变一些逻辑,因为即使我删除了新部分,无论我输入什么,它仍然无法工作。在这一点上,我想我只需要一双新眼睛指向正确的方向。

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

// Checks if the user input the correct number of arguments.
// If so, checks if they were input corecctly. If so, it assigns
// the user input values to rows/columns, and if not, prints
// an error message.
     if(argc == 5) {
            for(int i = 1; i < argc; i++) {
                    rows = getArg(argc, argv, i, compare1);
            }
            for(int i = 1; i < argc; i++) {
                    columns = getArg(argc, argv, i, compare2);
            }
    } else {
           printError(argv);
    }

这是主要的相关部分。

以下是检查错误所涉及的功能。我实际上一直在研究的是getArg,所以我假设这是逻辑失败的地方,但为了清晰起见,我还包括了其他必要的功能。

// Description: Checks if user input was valid
// Parameters: Command line arguments, int i from the for
// loop used to run this check on all command line arguments
// in main, and an array of chars used to compare the user's
// inputs to "-rows" or "-columns"
// Return value: If user input was valid, returns an int
// If not, exits program.

int getArg(int argc, char* argv[], int i, char compare[]) {

    int arg;
    if (strcmp(argv[i], compare) == 0) {
           if (isInt((i + 1), argv)) {
                    arg = atoi(argv[i + 1]);
            } else {
                    printError(argv);
            }
    } else {
            printError(argv);
    }

    return arg;
}

// Description: Checks user input for valid integers
// Parameters: Command line arguments
// Return value: Returns true if input is an int;
// false if not.

bool isInt(int argc, char* argv[]) {
    bool isInt;

    for (int j = 0; j < strlen(argv[argc]); j++) {  //For loop runs through each char in the array at argc
            if (isdigit(argv[argc][j])) {   // Checks to see if char is an integer
                    isInt = true;
                    return isInt;
            } else {
                    isInt = false; // If there is ever a non-integer character, exit loop and return false
                    return isInt;
            }
    }

}

3 个答案:

答案 0 :(得分:0)

我发现的一个可能的错误是您没有在isInt()函数中正确循环。

在第一次迭代后,你总是会退出函数。

答案 1 :(得分:0)

main中的第一个for循环在匹配“-rows”后继续扫描参数列表。循环进行以下调用(我假设compare1 =“ - rows”,因为你没有提到它):

i=1: getArg(argc=5, argv={"a.out", "-rows", "2", "-columns", "3"}, i=1, compare="-rows") 
    returns 2
i=2: getArg(argc=5, argv={"a.out", "-rows", "2", "-columns", "3"}, i=2, compare="-rows")
    calls printError(argv) because strcmp("2", "-rows") is nonzero

另外,正如Abhishek所说,isInt只检查字符串的第一个字符,因为你在isInt = true分支返回。

答案 2 :(得分:0)

很高兴你得到了答案,但这是一个非常容易使用的东西,在整个SO上你会发现这个建议,无论这些问题被标记为C ++

使用boost::program_options

#include <iostream>
#include <boost/program_options.hpp>

namespace po = boost::program_options;

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

    try {
        int rows,cols;
        po::options_description desc("Allowed options");
        desc.add_options()
            ( "help", "produce this help message" )
            ( "rows", po::value< int>(&rows)->required(), "No. of Rows" )
            ( "cols", po::value< int>(&cols)->required(), "No. of Cols"  )
        ;

        po::variables_map vm;
        po::store( po::parse_command_line( argc, argv , desc ), vm );
        po::notify( vm );


        if ( vm.count( "help" ) )
        {
            std::cout << desc;
            return 0;
        }

        std::cout<<"Rows :"<<rows<<" "<<"Cols :"<<cols<<std::endl;
    }
    catch( std::exception& e )
    {
        std::cout << e.what() << "\n";
        return 1;
    }    

    return 0;
}

用法: ./test --cols 4 --rows 3

输出:

Rows :3 Cols :4

好教程Here

相关问题