【问题标题】:Too few arguments in function函数中的参数太少
【发布时间】:2017-08-18 20:57:38
【问题描述】:

我正在编写一个函数 getIntLimited,它只能排除某个最大值和最小值之间的数字。目前没有指定最大值或最小值,但代码应该基本上可以正常工作。但是,我似乎收到一个错误,说函数中的参数太少,但我不确定为什么会这样。这是我的代码:

#include <stdio.h>
#include <stdlib.h>

//tools
void welcome(void);
void printTitle(void);
void printFooter(double gTotal);
void flushKeyboard(void);
void pause(void);
int getInt(void);
double getDouble(void);
int getIntLimited(int lowerLimit, int upperLimit);

//app interface
int yes(void);
void GroceryInventorySystem(void);
int menu(void);

int main(void){
    int iVal;
    double dVal;
    welcome();
    printTitle();
    double grandtotal = 1234.57;
    printFooter(grandtotal);
    flushKeyboard();
    pause();
    getInt();
    int lowLimit;
    int upLimit;
    getIntLimited(int lowLimit, int upLimit);

    return 0;
}


//code your functions here:

void welcome(void)
{
    printf("---=== Grocery Inventory System ===---");
    printf("\n");
    return;
}

void printTitle(void)
{
    printf("Row |SKU| Name          | Price |Taxed| Qty | Min |     Total    |Atn\n");
    printf("----+---+---------------+-------+-----+-----+-----+-------------|---");
    printf("\n");
    return;
}

void printFooter(double grandTotal)
{
    printf("--------------------------------------------------+-----------------");
    printf("\n");

    if (grandTotal > 0) {
    printf("                                     Grand Total: |     %12.2lf",  grandTotal);
            }
    printf("\n");
                                        return;
}


void flushKeyboard(void)
{
    int read;

    while (( read = getchar()) != '\n')
    return;
}

void pause(void)
{
    printf("Press <ENTER> to continue...\n");
    flushKeyboard();
    return;
}

int getInt(void)
{
    int Value;
    char NL = 'x';

    while (NL != '\n') {
            scanf("%d%c", &Value, &NL);

if (NL != '\n') {
            flushKeyboard();
            printf("Invalid integer, please try again: \n");
        }
    }
    return Value;
}

int getIntLimited(int lowerLimit, int upperLimit)
{
    int limit;
    do {
            limit = getInt();

            if(lowerLimit > limit || limit > upperLimit) {
                    printf("Invalid value, %d < %d < %d: ", lowerLimit, limit,   upperLimit);
            }
    }

            while(lowerLimit < limit && limit < upperLimit);
    return limit;

}                             

【问题讨论】:

  • 只是一般性评论:与其用自己的话总结错误消息,不如复制/粘贴实际的错误消息更有价值。

标签: c function parameters arguments


【解决方案1】:

在您的 main 函数中,这不是函数调用:

getIntLimited(int lowLimit, int upLimit);

去掉int关键字:

getIntLimited(lowLimit, upLimit);

另外,请注意lowLimitupLimit 在传递给getIntLimited 时不会被初始化。读取未初始化的值会调用未定义的行为。

【讨论】:

    【解决方案2】:

    getIntLimited(int lowLimit, int upLimit);而不是这个使用 getIntLimited(lowLimit, upLimit);因为函数调用期间不需要数据类型

    【讨论】:

      猜你喜欢
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 2017-04-01
      • 2018-04-22
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多