我的简单旅行计划的代码如下,我一直在圈子里四处走动。我可以把它归结为一个错误,它说:
(变量'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");
}
答案 0 :(得分:4)
您没有在fuel
中为局部变量AskUserForInput()
提供值。
答案 1 :(得分:1)
什么是导致错误而不是警告的编译器设置,BTW?但是,是的,你正在使用未经初始化的燃料 -
PrintTripSummary(avgMiles, minCost, maxCost, travelMiles, fuel);
我发现基本上这并不重要,因为实际上该功能不使用燃料。但编译器没有看到它,可能不是在它下面声明它时,如果它在另一个编译单元中它绝对不能。因此,要么从参数列表中取出燃料,要么在拨打电话之前添加
fuel = 0.0f;
此外,请注意您代替:
void WelcomeMessage();
你真的想要:
void WelcomeMessage(void);
由于向后兼容的原因,C中的第一个没有声明参数的类型,并且无论放入什么内容都不会发出警告。
答案 2 :(得分:1)
一些事情:
AskUserForInput()
没有读取燃油值,因此当它传递给PrintTripSummary()
fuel
中可能不需要PrintTripSummary()
参数,而是在那里进行计算。只需删除参数,将燃料声明为局部变量并在那里计算fuel
printf("You will need to purchase %.2f gallons of fuel.\n");
醇>