调用[C程序]的函数参数太少

时间:2014-11-16 19:17:36

标签: c function arguments

所以,我明天晚上有一份到期的在线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);
}

2 个答案:

答案 0 :(得分:0)

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

//printtripSummary();

因为您已在askuserForInput();中调用该函数,并且此函数会在main中调用

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

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


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

答案 1 :(得分:0)

在第13行,你调用printtripSummary函数而不传递任何参数。您必须提供函数定义指定的4个参数(avgMilesminCostmaxCosttravelMiles)。