书中的命令行参数程序没有工作......(C)

时间:2014-05-18 05:25:22

标签: c pointers command-line-arguments codeblocks

接近第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;
    }

1 个答案:

答案 0 :(得分:3)

您需要提供命令行参数。

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

Project > Set programs' arguments...

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

Jupiter venus Earth fred
相关问题