2015-07-18 103 views
0

遇到我編寫的這個程序有問題。在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; 
    } 
+1

要調用'無效updateCost(雙newMiles,雙newPrice,雙newMpg)'幾次只有一個(不同的)說法。你必須提供所有三個。而且,你必須在任何調用之前實現函數,或者提供**函數原型**'void updateCost(double newMiles,double newPrice,double newMpg);'所以編譯器知道應該如何調用它。 –

+2

沒有人抱怨'雙主()'呢? –

+1

這是無效的C程序:'雙主()'。對於託管環境,簽名至少是'int main(void)'。 – Olaf

回答

0

在你getMilesDriven()方法,你打電話updateCost(miles);,但是你已經定義的方法需要三個參數:void updateCost (double newMiles, double newPrice, double newMpg)

0

您使用功能已申報前,將它們定義:

  • double getMilesDriven()
  • double pricePerGallonOfGas()
  • 等等

正在在其他功能中使用它們已被宣佈爲前/定義。嘗試在聲明使用後重新排列函數聲明。例如:

void updateCurrentMileage(double totalMiles) { 
    overallMiles += totalMiles; 
} 

void updateCost(double newMiles, double newPrice, double newMpg) { 

    overallMiles = newMiles; 
    overallMpg = newMpg; 
    overallPricePerGallon = newPrice; 
} 

double totalMiles() { 
    return overallMiles; 
} 

double myCarMpg() { 

    double myMpg; 

    printf("How many miles per gallon do you get on your car?:\n"); 
    scanf("%lf", &myMpg); 
    updateCost(myMpg); 
    return myMpg; 

} 

double costForTrip() { 

    cost = (overallMiles/overallMpg) * overallPricePerGallon; 

    return cost; 

} 

double totalSpentonGas() { 
    totalCost += cost; 
    return totalCost; 
} 

double pricePerGallonOfGas() { 

    double price; 

    printf("Enter price per gallon:\n"); 
    scanf("%lf", &price); 
    updateCost(price); 
    return price; 
} 

double getMilesDriven() { 

    double miles; 

    printf("Enter miles driven:\n"); 
    scanf("%lf", &miles); 
    updateCurrentMileage(miles); 
    updateCost(miles); 

    return miles; 
} 

另外,您不必初始化全局變量 - 它們將在編譯時初始化。

編輯:和其他人已經注意到double main()應該是int main()遵循適當的C程序,因爲主要應返回一個int(最簡單的說法)。

0

程序沒有線的數量,但是,問題是: 如果(overallMiles = 0),正確的是如果(overallMiles == 0) // 和功能updateCost,你有3個參數 updateCost(雙newMiles,雙newPrice,雙newMpg)

,但你把它傳遞一個參數,updateCost(英里);

,在這裏,你使用數組的最後一個位置,不會是一個好主意。 arr2 [500] =結果; 如果你不需要別人的職位,可以使用雙重變種。