【问题标题】:Command-line argument program from book doesn't work...(C)书中的命令行参数程序不起作用......(C)
【发布时间】:2014-05-18 13:27:43
【问题描述】:

在第 13 章即将结束时,一个演示应该打印的命令行参数的程序

Jupiter is planet 5
venus is not a planet
Earth is planet 3
fred is not a planet

根据输入

planet Jupiter venus Earth fred

我用我的章节代码检查了所有内容,我正在使用 Code:Blocks 并编译为 C99。

当我运行程序时,程序立即结束,要求按任意键退出。

    // Checks planet names

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

    #define NUM_PLANETS 9

    int main(int argc, char *argv[])
    {
        char *planets[] = {"Mercury", "Venus", "Earth",
                           "Mars", "Jupiter", "Saturn",
                           "Uranus", "Neptune", "Pluto"};
        int i, j;

        for (i = 1; i < argc; i++) {
            for (j = 0; j < NUM_PLANETS; j++)
                if (strcmp(argv[i], planets[j]) == 0) {
                    printf("%s is planet %d\n", argv[i], j + 1);
                    break;
                }
            if (j == NUM_PLANETS)
                printf("%s is not a planet\n", argv[i]);
        }

        return 0;
    }

【问题讨论】:

  • 感谢@Barmar 的编辑

标签: c pointers command-line-arguments codeblocks


【解决方案1】:

您需要提供命令行参数。

使用 CodeBlocks,您可以这样设置:

Project > Set programs' arguments...

这将打开一个窗口,您可以在其中插入参数。在这里输入:

Jupiter venus Earth fred

【讨论】:

    猜你喜欢
    • 2012-11-28
    • 2016-02-22
    • 1970-01-01
    • 2017-06-19
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    相关资源
    最近更新 更多