【问题标题】:Issues with functions in C and pointersC和指针中的函数问题
【发布时间】: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


【解决方案1】:

在您的 getMilesDriven() 方法中,您调用的是 updateCost(miles);,但是您已将该方法定义为需要三个参数:void updateCost (double newMiles, double newPrice, double newMpg)

【讨论】:

    【解决方案2】:

    您的程序没有行数,但是,问题是: if (overallMiles=0),正确的是if (overallMiles==0) // 和函数 updateCost,你有 3 个参数 updateCost(双倍 newMiles、双倍 newPrice、双倍 newMpg)

    但你称之为传递一个参数,updateCost(miles);

    在这里,您使用的是数组的最后一个位置,这不是一个好主意。 arr2[500]= 结果; 如果你不需要其他职位,可以使用双变量。

    【讨论】:

      【解决方案3】:

      在声明和定义函数之前,您正在使用函数:

      • 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 程序,因为 main 应该返回一个 int(用最简单的术语来说)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-31
        • 1970-01-01
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        • 2021-03-04
        相关资源
        最近更新 更多