在预期浮点值的地方使用的指针值

时间:2017-10-23 05:47:53

标签: c pointers compiler-errors floating-point

我一直收到这个错误:

pointer value used where a floating point value was expected

我无法理解为什么。我确信它很容易。我只是累了,不能连接点,我的大脑保持微睡。如果一双新鲜的眼睛可以看一眼,并向我解释为什么我会遇到这个问题。感谢。

哦,错误与:payment = (float)paymentInput;行有关。

void purchaseItem(VmSystem * system)
{ 
    char purchThis[ID_LEN+NULL_SPACE];
    printf("Please input ID of item you wish to purchase: ");
    fgets(purchThis, ID_LEN+NULL_SPACE, stdin);
    printf("\n");

    Node * previousNode, * currentNode;

        previousNode = NULL;
        currentNode = system->itemList->head;
        while(currentNode != NULL)
        {
            if(strcmp(purchThis, currentNode->data->id) == 0)
            {
                if(currentNode->data->onHand > 0)
                {
                    char paymentInput[4];
                    float payment;
                    float itemValueFloat;
                    long itemValue;
                    float remaining;
                    float change;
                    /*
                    float validDenom = 1000, 500, 200, 100, 50, 20, 10, 5;
                    */


                    long x = currentNode->data->price.dollars;
                    long y = currentNode->data->price.cents;

                    itemValue = (x*100)+y;

                    itemValueFloat = (float)itemValue;
                    itemValueFloat = itemValueFloat/100;
                    printf("Item value in cents is: %ld cents.", itemValue);

                    printf("You have selected \"%s   %s\”. This will cost you $%.2f.\n", currentNode->data->name, currentNode->data->desc, itemValueFloat);

                    printf("Please hand over the money - type in the value of each note/coin in cents.\n");
                    printf("Press enter on a new and empty line to cancel this purchase:\n");
                    printf("You still need to give us $%.2f:", itemValueFloat);
                    fgets(paymentInput, 4, stdin);

                    payment = (float)paymentInput;

                    checkDenomValidity(payment, itemValueFloat);
                    itemValueFloat = itemValueFloat*100;

                    while(itemValueFloat - payment > 0)
                    {
                        remaining = (itemValueFloat - payment)/100;
                        itemValueFloat = remaining;
                        printf("You still need to give us $%.2f: ", itemValueFloat);

                        fgets(paymentInput, 4, stdin);
                        payment = (float)paymentInput;

                        checkDenomValidity(payment, itemValueFloat);
                        itemValueFloat = itemValueFloat*100;

                    }
                    if(itemValueFloat - payment <= 0)
                    {
                        change = (itemValueFloat - payment)*-1; 
                        change = change/100;
                        printf("Thank you. Here is your %s, and your change of $%.2f.\n", currentNode->data->name, change);
                        printf("Please come back soon.");
                    }

                    currentNode->data->onHand--;
                    break;
                } 

                else
                {
                    printf("OOPS! SORRY, WE ARE SOLD OUT OF THAT ITEM,\n");
                    printf("Please select a different item: ");

                    fgets(purchThis, ID_LEN+NULL_SPACE, stdin);
                }


                break;

            }

            previousNode = currentNode;
            currentNode = currentNode->next;
        }

        if(currentNode == NULL)
        {
            return;
        }

        readRestOfLine();
        return;
}

这个函数也有错误:

float checkDenomValidity(float payment, float itemValueFloat)
{
    char paymentInput[4];

    while(payment != 1000 || payment != 500 || payment != 200 || payment != 100 || payment != 50 || payment != 20 || payment != 10 || payment != 5)
        {
            payment = payment/100;
            printf("Error: $%.2f is not a valid denomination of money.", payment);

            printf("You still need to give us $%.2f:", itemValueFloat);
            fgets(paymentInput, 4, stdin);

            payment = (float)paymentInput;
        }
    return payment;
}

1 个答案:

答案 0 :(得分:3)

您无法使用强制转换将char[]投射到浮动广告 使用atof()