-1

Good morning, guys! I'm currently a newly started programming learner. Down here is my code for a mini app store. However, there is a problem going on, yet I couldnt locate the problem. The problem happen when I tried buying an app for $89.99 and I chose to redeem $10 9 times so I would have enough money to purchase the app (I didnt choose the $100 option because it would work just fine). However, the remained amount became $-79.99 instead of $0.01. Like I said, if I chose to deposit $100, the remained balance would be $10.01, which is normal. I don't get where I did wrong. Here is my code!

#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);
}
  • 3
    Please see [here](http://stackoverflow.com/help/mcve) on how to create a _minimal, complete, and verifiable example_ (with emphasis on *minimal*). The long and short of it is: don't just dump your entire code here - narrow it down to a small section of code that reproduces the issue and give us that. – R_Kapp Oct 26 '16 at 14:28
  • 1
    After the while(Compare()) you make deposit = depositPtr, that erases all the deposits you made and make it only worth the last one – fernando.reyes Oct 26 '16 at 14:31
  • 1
    Note: **Never** use floating point types if you need **exact** values. They cannot represent almost all fractional values exactly. Use scaled integers for currencies. – too honest for this site Oct 26 '16 at 14:36
  • Do not ever, under any circumstances, not even in "I'm learning" test code, for any reason whatsoever use `float` or `double` when doing currency calculations. In fact, since you're learning, count other things. Don't touch money. Money is special, it is not floating point. Learn on anything else. Please. Money is really special and requires some knowledge to get right and if you learn bad habits when dealing with money bad things will happen. `Money is not floating point.` – Art Oct 26 '16 at 14:37
  • Possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – too honest for this site Oct 26 '16 at 14:39
  • If you insist on learning with money anyway: use integers and count cents, not dollars. You won't get all rounding right all the time, but it will be correct in 99.9% of the cases. Floating points will almost never be correct. – Art Oct 26 '16 at 14:39
  • Thank you for your advice! I will keep this in mind. – user6866732 Oct 26 '16 at 14:41
  • Advice to not use `double` for currency is simply calling that approach bad without providing alternatives. All approaches in C have their short-comings, not only `double`. The best is to clearly understand and code for those issues - as with all programming tasks. [This](http://stackoverflow.com/questions/32212213/how-to-represent-currency-or-money-in-c/32214586#32214586) discusses some of those currency issues. – chux - Reinstate Monica Oct 26 '16 at 15:35

1 Answers1

2

In this code : GetChange(&depositPtr, choiceCost); You shold pass deposit (total deposit) and not &depositPtr (last deposit, only 10)

Isukthar
  • 185
  • 8