【发布时间】:2016-04-18 23:38:40
【问题描述】:
这是一个打印一些关于我自己的信息的程序 在这个程序中,如果用户扫描 /n 它打印名称和 等等,但是当我使用 gcc 运行这个程序时,它什么也没打印。 我需要使用 argv 和 argc 扫描参数。 我该如何解决?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
int main(int argc, char** argv)
{
for (int i = 1; i < SIZE; i++)
{
if (argv[i] == ' ')
{
break;
}
if (argv[i] == 'n' || argv[i] == 'b' || argv[i] == 'f' || argv[i] == '?' && argv[i - 1] == '/')
{
switch (i)
{
case 'n':
printf("my name is : Daniel Zingerman \n");
break;
case 'b':
printf("my birth date is: 2/11 \n");
break;
case 'f':
printf("my favorite food is: ice cream \n");
break;
case '?':
printf("the instruction of the program:");
printf("There is a lot of parameters you can scan into the program:");
printf("1. /n - printing the name");
printf("2. /b - printing the birth date");
printf("3. /n - printing the favorite food");
printf("4. /? - printing the instructions");
break;
}
}
}
system("pause");
return(0);
}
【问题讨论】:
-
你遇到这个程序的第一个惊喜是什么?
-
使用 string.h 中的 strcmp 比较字符串。 argc 应该限制你的 for 循环,而不是 SIZE。
-
case '/n':到底是什么? -
还有更严重的问题,程序无法编译。
-
当命令行参数少于 10 个时会发生什么?
标签: c switch-statement argv argc