存款给出负数?

时间:2016-10-26 14:25:23

标签: c

早上好,伙计们!我目前是一名新开始的编程学习者。这里是我的迷你应用程序商店的代码。但是,有一个问题正在发生,但我找不到问题。 问题发生在我尝试以89.99美元购买应用程序并且我选择兑换10美元9次以便我有足够的钱购买应用程序(我没有选择100美元的选项,因为它会工作得很好)。然而,剩余金额变为$ -79.99而不是0.01美元。就像我说的,如果我选择存入100美元,剩余的余额将是10.01美元,这是正常的。我不知道我做错了什么。这是我的代码!

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>

int Compare(double deposit, double choiceCost);
void DisplayApps(char *selectionPtr);
void SetCost(char selection, double *costPtr);
void PaymentOptions(double *depositPtr,double cost);
void DoItAgain(char *quitPtr);
//void Pay(double *depositPtr, double choiceCost);
void GetChange(double *depositPtr, double choiceCost);
void DoItAgain(char *quitPtr);

int main()
{
    char selectionPtr;
    char selection;
    char quitPtr;
    double costPtr;
    double deposit = 0.0;
    double choiceCost;
    double depositPtr = 0.0;
    double cost = 0.0;

    printf("Welcome to the App Store\n");
    printf("***************************\n\n");
    printf("Your deposit is: %.2f\n", deposit);
    printf("\n");

    while (1)
    {
        do {
            DisplayApps(&selectionPtr);

            selection = selectionPtr;
            SetCost(selection, &costPtr);

            choiceCost = costPtr;
            Compare(deposit, choiceCost);

            while (Compare(deposit, choiceCost) == 0)
            {
                printf("Your balance isn't enough.\n");
                printf("In order to purchase this item, you have to redeem more money.\n");
                PaymentOptions(&depositPtr, cost);
                deposit += depositPtr;
                printf("You have redeemed $%.2f\n", depositPtr);
                printf("Your balance now is: $%.2f\n", deposit);
                printf("\n");
            }
            deposit = depositPtr;

            GetChange(&depositPtr, choiceCost);

            DoItAgain(&quitPtr);
        } while (quitPtr == 'Y' || quitPtr == 'y');
        return 1;
    }

    return 0;
}

void DisplayApps(char *selectionPtr)
{
    printf("-------------------------\n");
    printf("HERE ARE THE SLECTIONS\n");
    printf("C -- Clown Punching             $299.99\n");
    printf("V -- Virtual Snow Globe         $349.99\n");
    printf("R -- Remote PC                  $999.99\n");
    printf("G -- Grocery List Helper        $2.99\n");
    printf("M -- Mobile Cam Viewer          $89.99\n");
    printf("\n");

    printf("Please enter a selection: ");
    scanf(" %c", &*selectionPtr);
    printf("\n");

}

void SetCost(char selection, double *costPtr)
{
    if (selection == 'C' || selection == 'c')
    {
        *costPtr = 299.99;
    }
    else if (selection == 'V' || selection == 'v')
    {
        *costPtr = 349.99;
    }
    else if (selection == 'R' || selection == 'r')
    {
        *costPtr = 999.99;
    }
    else if (selection == 'G' || selection == 'g')
    {
        *costPtr = 2.99;
    }
    else if (selection == 'M' || selection == 'm')
    {
        *costPtr = 89.99;
    }
}

int Compare(double deposit, double choiceCost)
{
    if (deposit < choiceCost)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

void PaymentOptions(double *depositPtr, double cost)
{
    printf("You have (4) options to choose:\n");
    printf("1 - $1000.00\n");
    printf("2 - $500.00\n");
    printf("3 - $100.00\n");
    printf("4 - $10.00\n");
    printf("How much do you want to redeem?");
    printf(">>>>> ");
    scanf("%lf", &cost);
    printf("\n");
    printf("-------------------------------------\n");
    if (cost == 1)
    {
        *depositPtr = 1000.00;
    }
    else if (cost == 2)
    {
        *depositPtr = 500.00;
    }
    else if (cost == 3)
    {
        *depositPtr = 100.00;
    }
    else if (cost == 4)
    {
        *depositPtr = 10.00;
    }

}

void GetChange(double *depositPtr, double choiceCost)
{
    *depositPtr = *depositPtr - choiceCost;
    printf("You have purchased this item successfully.\n");
    printf("You still have $%.2f remained in you balance.\n", *depositPtr);
}

void DoItAgain(char *quitPtr)
{
    printf("Do you want to continue purchase another item? [Y/N]\n");
    scanf(" %c", &*quitPtr);
}

1 个答案:

答案 0 :(得分:2)

在此代码中:GetChange(&depositPtr, choiceCost); 您通过deposit(总存款),而不是&depositPtr(最后存款,只有10)