从main调用void函数时不兼容的类型

时间:2013-04-19 00:25:11

标签: c

我正在写一个程序,你有一定数量的钱,你可以用这些钱购买柠檬水袋来制作柠檬水。我有一个功能:

    void buy_lemons(double *pLemons, double *pMoney);

每当我从main调用它时,都会给我这个错误:

    error: incompatible type for argument 1 of 'buy_lemons'

在main之前的函数原型中有另一个注释:

    note: expected 'double *' but argument is of type 'double'

到目前为止,这是我的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    // Symbolic constants to be used.

    // Prices of bags of sugar and lemon, respectively.
    #define PRICE_LEMON 3.50
    #define PRICE_SUGAR 2.00

    // Fraction of a bag of lemons and sugar used on a single cup of lemonade.
    #define LEMON_PER_CUP 0.03
    #define SUGAR_PER_CUP 0.04

    // The initial loan the user is given to start their lemonade stand.
    #define START_MONEY 20.00

    // Using symbolic constants for true and false.
    #define FALSE 0
    #define TRUE 1

    // Function prototypes - do not change these
    void buy_lemons(double *pLemons, double *pMoney);
    void buy_sugar(double *pSugar, double *pMoney);

    // Main function
    int main() {

      int num_day, ans, climate, cost;
      double money = START_MONEY, num_lemons = 0.00, num_sugar = 0.00;

      srand(time(0));

      printf("Welcome to the Game of Lemonade!\n\n");
      printf("You start the game with $%.2lf and no supplies!\n", START_MONEY);

      // Loop through each day. Ask the user if they want to buy lemons. If so,
      // carry out the transaction. Then ask them if they want to buy sugar.
      // If so, do this transaction as well. Then, let them sell lemonade for
      // the day. Finally, print a status report after they've sold lemonade
      // at the end of the day.

      for (num_day = 1; num_day <= 10; num_day++) {
            printf("Would you like to buy some lemons? (1 - Yes, 0 - No)\n");
            scanf("%d", &ans);
            if(ans == 1)
                buy_lemons(num_lemons, money);
                printf("1a\n");

            if(ans == 0)
                printf("2a\n");

            printf("Would you like to buy some sugar? (1 - Yes, 0 - No)\n");
            scanf("%d", &ans);
            if(ans == 1)
               buy_sugar(num_sugar, money);
               printf("1b\n");

            if(ans == 0)
                printf("2b\n");
      }

      return 0;
    }

    // Pre-conditions: pLemons and pMoney are pointers to variables that store
    //                 the user's number of bags of lemons left and amount of
    //                 money left.
    // Post-condition: The user is given the opportunity to buy lemons. If
    //                 successful, the number of bags of lemons and the amount
    //                 of money the user has are adjusted accordingly.
    //
    // What to do in this function: If the user doesn't have enough money to
    // even buy one bag of lemons, tell them so and return. Otherwise, ask
    // the user how many bags of lemons they want to buy. If they answer less
    // than one, tell them they must get more and reprompt them. If they
    // answer more than they can buy, tell them they don't have that much
    // money and reprompt them. Continue prompting them until they answer with
    // a valid value. Then process the transaction.

    void buy_lemons(double *pLemons, double *pMoney) {
        double bags, total;


        if (*pMoney < PRICE_LEMON)
            printf("You do not have enough money to buy any lemons.\n");

        if(*pMoney >= PRICE_LEMON){
            printf("How many bags of lemons would you like to purchase?\n");
            scanf("%.2lf\n", &bags);

                while (bags < 1){
                    printf("You must buy at least 1 bag of lemons.\n");
                    printf("How many bags of lemons would you like to purchase?\n");
                    scanf("%.2lf\n", &bags);
                }

            total = PRICE_LEMON * bags;
            printf("The total is %lf.\n", &total);

                while(total > *pMoney){
                    printf("You don't have enough money.\n");
                    printf("How many bags of lemons would you like to purchase?\n");
                    scanf("%.2lf\n", &bags);

                    total = PRICE_LEMON * bags;
                }

            *pLemons += bags;
            *pMoney -= total;
        }

    }

    // Pre-conditions: pSugar and pMoney are pointers to variables that store
    //                 the user's number of bags of lemons left and amount of
    //                 money left.
    // Post-condition: The user is given the opportunity to buy sugar. If
    //                 successful, the number of bags of sugar and the amount
    //                 of money the user has are adjusted accordingly.
    //
    // What to do in this function: If the user doesn't have enough money to
    // even buy one bag of sugar, tell them so and return. Otherwise, ask
    // the user how many bags of sugar they want to buy. If they answer less
    // than one, tell them they must get more and reprompt them. If they
    // answer more than they can buy, tell them they don't have that much
    // money and reprompt them. Continue prompting them until they answer with
    // a valid value. Then process the transaction.

    void buy_sugar(double *pSugar, double *pMoney) {
        double bags, total;

        if (*pMoney < PRICE_SUGAR)
            printf("You do not have enough money to buy any sugar.\n");

        if(*pMoney >= PRICE_SUGAR){
            printf("How many bags of sugar would you like to purchase?\n");
            scanf("%.2lf\n", &bags);

                while (bags < 1){
                    printf("You must buy at least 1 bag of sugar.\n");
                    printf("How many bags of sugar would you like to purchase?\n");
                    scanf("%.2lf\n", &bags);
                }

            total = PRICE_SUGAR * bags;
            printf("The total is %lf.\n", &total);

                while(total > *pMoney){
                    printf("You don't have enough money.\n");
                    printf("How many bags of sugar would you like to purchase?\n");
                    scanf("%.2lf\n", &bags);

                    total = PRICE_SUGAR * bags;
                }

            *pSugar += bags;
            *pMoney -= total;
        }
    }

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

而不是

buy_lemons(num_lemons, money);

尝试

buy_lemons(&num_lemons, &money);

推理:&amp;放在变量名称之前的是'address-of'运算符。如果将地址传递给整数而不是数字值,则可以将其解释为指针并取消引用,以便即使在存储它的原始位置也可以编辑该值。

e.g。如果num_lemons的类型为double,则&num_lemons的类型为double*

P.S。这就是为什么你把&amp;在非指针参数传递给scanf

之类的函数之前