c - void 语句的问题

标签 c void

我的一个简单旅行程序的代码如下,我一直在上面兜圈子。我可以把它归结为一个错误:

(正在使用变量 'fuel' 而未初始化)...

我被卡住了,正在寻找下一步该做什么的提示/提示或帮助。如果您对此有任何疑问,请提出,如果有任何使它成为更好的帖子的提示,请告诉我。

谢谢

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

void WelcomeMessage();
void AskUserForInput();
void PrintTripSummary(float avgMiles, float minCost, float maxCost, float travelMiles, float fuel);

int main()
{
    WelcomeMessage();
    AskUserForInput();
    printf("\nThank you, please drive safely and have a nice trip!\n");
    return 0;
}

void WelcomeMessage()
{
    printf("Welcome to the Trip Planner!");
    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, fuel;
    do {
        printf("Input your car's average miles per gallon (enter 0 to quit) ");
        scanf("%f", &avgMiles);
        if (avgMiles == 0)
            break;
        printf("The lowest estimated price per gallon of fuel is: ");
        scanf("%f", &minCost);
        printf("The highest estimated price per gallon of fuel is: ");
        scanf("%f", &maxCost);
        printf("How many miles you plan to travel: ");
        scanf("%f", &travelMiles);
        PrintTripSummary(avgMiles, minCost, maxCost, travelMiles, fuel);
    } while (avgMiles != 0);
}

void PrintTripSummary(float avgMiles, float minCost, float maxCost, float travelMiles, float fuel)
{
    fuel = avgMiles/travelMiles;
    minCost = fuel*minCost;
    maxCost=fuel*maxCost;
    printf("== == == == == == == = Trip Summary == == == == == == == == == == \n\n");
    printf("You will need to purchase %.2f gallons of fuel.\n");
    printf("The approximate cost of fuel for your trip is between $%5.2f and $%5.2f \n",&minCost,&maxCost);
    printf("Thank you, please drive safely and have a nice trip!\n\n");
    printf("== == == == == == == = End Trip Summary == == == == == == == == ==\n\n");
}

最佳答案

您没有在 AskUserForInput() 中为局部变量 fuel 赋值。

关于c - void 语句的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22315499/

相关文章:

c - 错误 : lvalue required as left operand of assignment, 错误:无效值没有被忽略,因为它应该是

c - 如何在二维整数数组上使用 malloc

C 编程数组编译时错误

c - 字符串存储到共享内存后无法正常显示

在 C 中从 main 调用 void* 函数

c - (void)var 实际上做了什么?

python - 使用 if-return-return 还是 if-else-return 效率更高?

c - 跳过第一个输入,直接转到下一个输入

c# - 防止任务在运行时再次执行

c# 从异步函数返回数据