【发布时间】:2015-07-18 20:43:32
【问题描述】:
我写的这个程序有问题。在 updateCost(miles) 和其他 update___() 中,我收到函数调用中的参数太少的错误。我也收到错误 5 错误 C2371: 'updateCurrentMileage' : redefinition;不同的基本类型以及相同的错误更新成本。我是传递函数的新手,所以我有点新手。我也想知道我的阵列是否正在做他们应该做的事情,我无法判断,因为我无法运行程序。关于它为什么这样做以及如何解决它的任何帮助都会很棒,因为我正在努力成为一个更好的程序员。干杯。
#include <stdlib.h>
#include <stdio.h>
double arr[500];
double arr2[500];
double arr3[500];
double cost= 0.0;
double totalCost= 0.0;
double overallMiles = 0.0;
double overallMpg = 0.0;
double overallPricePerGallon = 0.0;
int choice;
int quit= 0;
int menuChoice () {
int choice;
printf(" T a k e a D r i v e \n");
printf("1.Enter miles driven\n");
printf("2.How much is price per gallon of gas?\n");
printf("3.How many miles do you get per gallon?\n");
printf("4.Total cost for trip\n");
printf("5.How many miles have you drove in total?\n");
printf("6.How much money have you spent total on gas?\n");
printf("7.Quit\n");
printf("Enter your choice: ");
scanf("%i", &choice);
return choice;
}
double getMilesDriven() {
double miles;
printf("Enter miles driven:\n");
scanf("%lf", &miles);
updateCurrentMileage(miles);
updateCost(miles);
return miles;
}
void updateCurrentMileage(double totalMiles) {
overallMiles+=totalMiles;
}
double totalMiles () {
return overallMiles;
}
double pricePerGallonOfGas() {
double price;
printf("Enter price per gallon:\n");
scanf("%lf", &price);
updateCost(price);
return price;
}
double myCarMpg() {
double myMpg;
printf("How many miles per gallon do you get on your car?:\n");
scanf("%lf", &myMpg);
updateCost(myMpg);
return myMpg;
}
void updateCost (double newMiles, double newPrice, double newMpg) {
overallMiles = newMiles;
overallMpg = newMpg;
overallPricePerGallon = newPrice;
}
double costForTrip() {
cost = (overallMiles / overallMpg) * overallPricePerGallon;
return cost;
}
double totalSpentonGas() {
totalCost+= cost;
return totalCost;
}
double main () {
{
while(quit!=1) //begin menu loop
{
int menu;
menu = menuChoice();
//begin switch
switch(menu)
{
case 1:
{
double result = getMilesDriven();
arr[500]= result;
break;
}
case 2:
{
double result= pricePerGallonOfGas();
arr2[500]= result;
break;
}
case 3:
{
double result= myCarMpg();
arr3[500]= result;
break;
}
case 4:
if (overallMiles= 0)
printf("Please enter miles driven, mpg and cost per gallon first!\n");
else
printf("Cost of trip is %lf\n", costForTrip());
break;
case 5:
if (overallMiles= 0)
printf("You haven't even drove any miles!\n");
else
printf("Current total mileage is %lf\n", totalMiles());
break;
case 6:
if (cost = 0)
printf("You haven't spent any money on gas!\n");
else
printf("Total spent on gas is %lf\n", totalSpentonGas());
break;
case 7:
quit = 1;
break;
default:
printf("Please enter 1 through 6\n");
break;
}
}
return 0;
}
【问题讨论】:
-
您多次调用
void updateCost (double newMiles, double newPrice, double newMpg),但只有一个(不同的)参数。您必须提供所有三个。而且,您必须在任何调用之前实现该函数,或者提供 函数原型void updateCost (double newMiles, double newPrice, double newMpg);以便编译器知道应该如何调用它。 -
还没有人抱怨
double main()? -
那不是有效的 C 程序:
double main ()。对于托管环境,签名至少为int main(void)。 -
更令人惊讶的是,要求输入 mpg 并且不让程序计算它。程序中有一个除法:注意除以0
-
感谢您指出这一点,我什至没有意识到我有双 main ()。完全是无意的。
标签: c function pointers double