我的一个简单旅行程序的代码如下,我一直在上面兜圈子。我可以把它归结为一个错误:
(正在使用变量 '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/