0

Down here is a part of my current code (still incomplete). I was trying to assign the amount of cost to *costPtr. But after assigning it, I test the result in the main function to see if I got the right value for my choice, the result appeared to be 0.00001 instead of the value as assigned (299.9 or 349.99 or 999.99). What's wrong? I couldn't find the answer.

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

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

int main()
{
    char selection;
    char *selectionPtr;
    //double choiceCost;
    double *costPtr;
    //double *depositPtr;
    //char *quitPtr;

    printf("Welcome to the App Store\n");
    printf("***************************\n\n");
    DisplayApps(&selectionPtr);
    selection = selectionPtr;
    printf("Your choice %c\n", selection);
    SetCost(selection, &costPtr);
    printf("The cost of this item is %fl\n", costPtr);

    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: ", *selectionPtr);
    scanf(" %c", &*selectionPtr);

}

void SetCost(char selection, double *costPtr)
{
    double c = 299.99;
    double v = 349.99;
    double r = 999.99;
    double g = 2.99;
    double m = 89.99;
    if (selection == "C" || selection == "c")
    {
        *costPtr = c;
    }
    else if (selection == "V" || selection == "v")
    {
        *costPtr = v;
    }
    else if (selection == "R" || selection == "r")
    {
        *costPtr = r;
    }
    else if (selection == "G" || selection == "g")
    {
        *costPtr = g;
    }
    else if (selection == "M" || selection == "m")
    {
        *costPtr = m;
    }
}
  • 1
    Without even looking at your code, I can almost certainly conclude that this is a *floating point issue.* See [Why are floating point numbers inaccurate?](http://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Robert Harvey Oct 19 '16 at 14:59
  • Welcome to the joys of floating point inaccuracy. It's going to bug the rest of your working life if you become a developer. Here's something to read: http://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples – Robinson Oct 19 '16 at 15:00
  • This doesn't appear to be a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve). Replacing your code with one of those would make confirming that Robert Harvey is almost certainly right much faster. – Useless Oct 19 '16 at 15:01
  • The `double` type is incapable of representing the number 2.99, instead, you get something closer to 2.9900000000000002131628207. The same is true for the other decimals. (The computer is binary.) – Dietrich Epp Oct 19 '16 at 15:02
  • *"the result appeared to be 0.00001 instead of the value as assigned (299.9 or 349.99 or 999.99)"* – dang, that's a *really* big floating point inaccuracy. – JJJ Oct 19 '16 at 15:03
  • Thank you, guys! I'm trying to read it right now. This may help me somehow. – user6866732 Oct 19 '16 at 15:03
  • 1
    There are several layers of bugs in this code - fixing one only reveals more problems... – el.pescado - нет войне Oct 19 '16 at 15:05
  • 2
    I mean, uninitialized pointers, pointer type mismatch etc. This code should crash, but is too buggy to do so;) Turn on compiler warnings and read them. – el.pescado - нет войне Oct 19 '16 at 15:07
  • "result appeared to be 0.00001" --> Not possible for output to be `"0.00001"`. Post the _exact_ textual output of `printf("The cost of this item is %fl\n", costPtr);` Hint: Are you getting `"0.000000l"`? Look closely at the format. – chux - Reinstate Monica Oct 19 '16 at 15:45
  • I started from the scratch and made it worked. Thanks everyone for suggestion and help :) – user6866732 Oct 19 '16 at 15:49
  • 1
    @RobertHarvey That's why a reasonable person looks at the question rather than just the title. –  Oct 19 '16 at 16:46
  • I asked my teacher and she pointed out that it was because I wrote "C" instead of 'C'... I fixed it and it worked ^^! – user6866732 Oct 21 '16 at 01:06

2 Answers2

2

Your program, is malformed and with Undefined Behaviour, because you are calling to to SetCost with a double** on a double* argument.

You should better declare the cost variable as a normal double in main:

int main()
{
    // ...
    double cost;
    // ...
    SetCost(selection, &cost);
    printf("The cost of this item is %fl\n", cost);
    // ...
}
vz0
  • 32,345
  • 7
  • 44
  • 77
0

In main, declare double cost and pass &cost to SetCost. Then print cost. There are problems here that your compiler could have caught if warnings were enabled. For example, you're passing a double ** to SetCost when it's looking for a double *.

Andy Schweig
  • 6,597
  • 2
  • 16
  • 22