遇到我編寫的這個程序有問題。在updateCost(英里)和其他更新___()我得到的錯誤,在函數調用中的參數太少。我也收到錯誤5錯誤C2371:'updateCurrentMileage':重新定義;不同的基本類型以及無效更新成本中的相同錯誤。我是傳遞函數的新手,所以我相當有點新手。我也想知道我的數組是否正在做他們應該做的事情,我不能說因爲我不能運行程序。任何關於爲什麼這樣做以及如何修復它的幫助都會很好,因爲我正在努力成爲一名更好的程序員。乾杯。C中的函數問題和指針
#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;
}
要調用'無效updateCost(雙newMiles,雙newPrice,雙newMpg)'幾次只有一個(不同的)說法。你必須提供所有三個。而且,你必須在任何調用之前實現函數,或者提供**函數原型**'void updateCost(double newMiles,double newPrice,double newMpg);'所以編譯器知道應該如何調用它。 –
沒有人抱怨'雙主()'呢? –
這是無效的C程序:'雙主()'。對於託管環境,簽名至少是'int main(void)'。 – Olaf