检查命令行参数 - C编程

时间:2015-09-07 23:23:53

标签: c if-statement command-line-arguments

我正在尝试编写代码,说明是否有命令行输入,然后做一些事情,如果不做其他事情。

int main(int argc, char *argv[])
{
    int lowerBound, upperBound, i, count = 0;
    float val;
    char c;

    if(argc = 2)
    {
        lowerBound = argv[1];
        printf("Lower bound = %d", lowerBound);

        item * curr, * head;
        head = NULL;

        do
        {
            printf("Enter a number: ");
            scanf("%f", &val);
            curr = (item *)malloc(sizeof(item));

            if(val >= lowerBound)
            {
                curr->num = val;
                curr->next = head;
                head = curr;
                count++;
            }

            getchar();
            printf("Want to enter another number (y/n):  ");
            scanf("%c", &c);
        } while( c != 'n' && c != 'N' );

        curr = head;

        float largest = findLargest(curr);
        float lowest = findSmallest(curr);
        float mean = findMean(curr, count);
        int ValuesAboveMean = valuesAboveMean(curr, mean);
        int ValuesBelowOrAtMean = valuesBelowOrAtMean(curr, mean);
        float median = findMedian(curr, count);
        show(count, lowest, largest, mean, median, ValuesAboveMean,    ValuesBelowOrAtMean);
    }
    else if(argc = 3)
    {
        lowerBound = argv[1];
        upperBound = argv[2];
        printf("Lower bound = %d", lowerBound);
        printf("Upper bound = %d", upperBound);
        item * curr, * head;
        head = NULL;

        do
        {
            printf("Enter a number: ");
            scanf("%f", &val);
            curr = (item *)malloc(sizeof(item));

            if(val >= lowerBound && val <= upperBound)
            {
                curr->num = val;
                curr->next = head;
                head = curr;
                count++;
            }

            getchar();
            printf("Want to enter another number (y/n):  ");
            scanf("%c", &c);
        } while( c != 'n' && c != 'N' );

            curr = head;

            float largest = findLargest(curr);
            float lowest = findSmallest(curr);
            float mean = findMean(curr, count);
            int ValuesAboveMean = valuesAboveMean(curr, mean);
            int ValuesBelowOrAtMean = valuesBelowOrAtMean(curr, mean);
            float median = findMedian(curr, count);
            show(count, lowest, largest, mean, median, ValuesAboveMean, ValuesBelowOrAtMean);
        }
        else if(argc = 1 or argc = 0 or argc = NULL)
        {
            item * curr, * head;
            head = NULL;

            do
            {
                printf("Enter a number: ");
                scanf("%f", &val);
                curr = (item *)malloc(sizeof(item));

                curr->num = val;
                curr->next = head;
                head = curr;
                count++;

            getchar();
            printf("Want to enter another number (y/n):  ");
            scanf("%c", &c);
        } while( c != 'n' && c != 'N' );

         curr = head;

        float largest = findLargest(curr);
        float lowest = findSmallest(curr);
        float mean = findMean(curr, count);
        int ValuesAboveMean = valuesAboveMean(curr, mean);
        int ValuesBelowOrAtMean = valuesBelowOrAtMean(curr, mean);
        float median = findMedian(curr, count);
        show(count, lowest, largest, mean, median, ValuesAboveMean, ValuesBelowOrAtMean);
        }

由于某种原因,它忽略了if语句中的内容并运行if中的任何内容。例如,即使我没有命令行参数,它仍然希望运行第一个,如果arg = 2。

我做错了什么,如何解决?

1 个答案:

答案 0 :(得分:2)

你需要制作if(argc == 2)

(而不是if(argc = 2),它将2分配给argc并且始终为true)。我从不喜欢C定义=对= =的方式。有些编译器会对此发出警告。