gpt4 book ai didi

c - 在需要浮点值的地方使用指针值

转载 作者:行者123 更新时间:2023-11-30 18:28:30 25 4
gpt4 key购买 nike

我不断收到此错误:

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

最佳答案

您不能使用转换将 char[] 转换为 float 。
使用atof()

关于c - 在需要浮点值的地方使用指针值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46882684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com