【问题标题】:How to get rid of this command-line argument error?如何摆脱这个命令行参数错误?
【发布时间】:2013-02-26 07:14:40
【问题描述】:

我用 C++ 编写了一个程序,它应该接受几个命令行参数,其中第一个是整数,其余三个是浮点数。处理该问题的代码部分如下所示:

_tmain(__in int argc, __in PZPWSTR argv)
{


    USHORT X, Y, Z, ZR, XR;                         // Position of several axes
    JOYSTICK_POSITION   iReport;                    // The structure that holds the full position data
    BYTE id=1;                                      // ID of the target vjoy device (Default is 1)
    UINT iInterface=1;                              // Default target vJoy device
    BOOL ContinuousPOV=FALSE;                       // Continuous POV hat (or 4-direction POV Hat)
    int count=0;
    DOUBLE Xr, Yr, Zr;


    // Get the ID of the target vJoy device
    if (argc>1 && wcslen(argv[1]))
        sscanf_s((char *)(argv[1]), "%d", &iInterface);

    sscanf_s((char *)(argv[2]), "%d", &Xr);
    sscanf_s((char *)(argv[3]), "%d", &Yr);
    sscanf_s((char *)(argv[4]), "%d", &Zr);

_tprintf("Acquired: X %d\nY %d\nZ %d\n", Xr, Yr, Zr);

the rest of the code ...}

我的问题是当我在命令行中通过编写调用程序时

name.exe 1 15 16 17

我得到 Xr = 15 但 Zr = 16 而 Yr 只是一个看似随机的巨大负数。

我知道这可能是一些非常基本的错误,但我无法找到是什么。感谢您的任何建议。

【问题讨论】:

  • 这是预处理过的c++吗?为什么main 不是main
  • @Rubens:这是特定于 MS 的东西(_tmainPZWSTR for argv)。

标签: c++ command-line-arguments


【解决方案1】:

您需要使用%lf 转换来读取双精度。您当前使用的%d 仅适用于ints。

【讨论】:

  • 不完全是,%ffloats,%lfdoubles。
  • @ŁukaszNiemier:糟糕——非常正确。谢谢。 (当相同的转换用于 printfscanf 的不同类型时,总是会感到困惑)。
猜你喜欢
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
相关资源
最近更新 更多