2015-07-18 79 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] =结果; 如果你不需要别人的职位,可以使用双重变种。