0

私は、一定の金額を持っていて、そのお金でレモンの袋を購入してレモネードを作るプログラムを書いています。私は機能を持っています:

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

メインから呼び出すたびに、次のエラーが表示されます。

    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;
        }
    }

何か案は?

4

1 に答える 1

3

それ以外の

buy_lemons(num_lemons, money);

試す

buy_lemons(&num_lemons, &money);

理由: 変数名の前に配置された & は、'address-of' 演算子です。アドレスを数値ではなく整数に渡すと、ポインタとして解釈され、逆参照されて、格納されていた元の場所でも値を編集できます。

例: が type の場合num_lemonsdoubleis&num_lemonsは typedouble*

psこれは、 & のような関数に渡される非ポインター引数の前に置くのと同じ理由ですscanf

于 2013-04-19T00:27:30.153 に答える