【问题标题】:Too few arguments in function to call [C program]函数中的参数太少而无法调用 [C 程序]
【发布时间】:2014-11-16 19:17:36
【问题描述】:

所以,明天晚上我的在线 C 编程课程的作业要交了,我现在的编码有些问题。我已经把代码带给了我的老师,但她似乎不明白她是花钱教我的,而不是告诉我我的代码有问题。如果有人可以查看代码并帮助我修复它,我将不胜感激。代码位于下方。调用 printtripSummary 时出现错误的位置在 main 中。

#include <stdio.h>


void welcomeMessage();
void askuserForInput();
void printtripSummary(float avgMiles, float minCost, float maxCost, float travelMiles);

int main()
{
    /* Call the functions */
    welcomeMessage();
    askuserForInput();
    printtripSummary();
    printf("\nThank you, please drive safely and have a nice trip!\n");
    return 0;
}


void welcomeMessage()
{
    printf("Welcome to the Trip Planner!\n");
    printf("So you are ready to take a trip? Let me help you plan for\n");
    printf("your fuels costs and required stops to fill up your tank.\n");
    printf("============================================================\n");
    printf("Please provide answers to the prompts below and I will\n");
    printf("display a summary for you when I have computed the results.\n");
    printf("============================================================\n");
}
void askuserForInput()
{
    float avgMiles, minCost, maxCost, travelMiles;
    do{
        printf("Please input your car's average miles per gallon (enter 0 to quit)>> ");
        scanf_s("%f", &avgMiles);
        if (avgMiles == 0)
            break;
        printf("Please tell me the range of fuel costs you expect to pay (per gallon>>)\n");

        printf("The lowest per gallon cost of fuel is>> ");
        scanf_s("%f", &minCost);
        printf("The highest per gallon cost of fuel is>> ");
        scanf_s("%f", &maxCost);
        printf("Please tell me how many miles you plan to travel>> ");
        scanf_s("%f", &travelMiles);
        printtripSummary(avgMiles, minCost, maxCost, travelMiles);
    } while (avgMiles != 0);
}

void printtripSummary(float avgMiles, float minCost, float maxCost, float travelMiles)
{
    float avgGal, mingasPrice, maxgasPrice;
    do{
        avgGal = travelMiles / avgMiles;
        mingasPrice = avgGal * minCost;
        maxgasPrice = avgGal * maxCost;
        printf("You will be required to purchase %.2f gallons of fuel.\n", avgGal);
        printf("The price will range between %2f and $%.2f.\n", mingasPrice, maxgasPrice);
    } while (avgMiles != 0);
}

【问题讨论】:

  • 如果我可以问,这个错误是如何让自己知道的?
  • 查看printtripSummary 函数的原型(和定义),然后查看您对该函数的调用。
  • main 中调用函数printtripSummary 你必须给parameters!您还可以在 askuserForInput 中调用函数,因此您只需在 main out 中注释函数调用
  • @JoachimPileborg 哦 :-)
  • “她花钱是为了教我,而不是告诉我我的代码有问题”。也许她认为这个明显的错误信息是你从解决中学到的东西,而不是她只是告诉你。

标签: c function arguments


【解决方案1】:

像这样(第 13 行)注释掉 main 中的函数调用:

//printtripSummary();

因为您已经在 askuserForInput(); 中调用了该函数,而该函数在 main 中被调用

或者如果你想在 main 中调用函数,你必须传递所需的参数,这些参数是:

(float avgMiles, float minCost, float maxCost, float travelMiles)


此外,您在函数 printtripSummary(); 中有一个无限循环,因为您有一个 do...while 循环来检查 avgMiles != 0,但由于您没有在此循环中更改 avgMiles 的值,所以它是无限循环的!

【讨论】:

  • 非常感谢。注释掉 printtripSummary 修复了它。
  • @Bioniclefreak25 欢迎您!祝你有美好的一天:D BTW 看看函数printtripSummary(); 因为你有一个无限循环!
【解决方案2】:

在第 13 行,您调用了 printtripSummary 函数,但没有向它传递任何参数。您必须提供函数定义指定的 4 个参数(avgMilesminCostmaxCosttravelMiles)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多