gpt4 book ai didi

c - 我正在创建一个简单的程序来计算涉及 if/else 的销售总额并不断收到错误消息

转载 作者:太空宇宙 更新时间:2023-11-04 06:33:49 25 4
gpt4 key购买 nike

该程序计算折扣(如果客户是教师)和销售税并计算总销售额。我不断收到错误消息:

[Warning] comparison between pointer and integer [enabled by default]

#include <stdio.h>
#include <math.h>

#define SALES_TAX .05
#define DISCOUNT_LOW .10
#define DISCOUNT_HIGH .12
#define DISCOUNT_LIMIT .100

int main(void)
{
double purchase_total;
double discount;
double discounted_total;
double sales_tax;
double total;
int teacher;
FILE* output_file;

/* request inputs */
printf("Is the customer a teacher (y/n)?");
scanf("%d", &teacher);
printf("Enter total purchases.");
scanf("%lf", &purchase_total);

/* calculations for teacher */
if (teacher == "y");
{/*calculate discount (10% or 12%) and 5% sales tax */
/* purchase total less than 100 */
if (purchase_total < 100)
{
/* calculate 10% discount */
discount = purchase_total * DISCOUNT_LOW;
discounted_total = purchase_total - discount;
}

/*purchase total greater than 100 */
else
{ /* calculate 12% discount */
discount = purchase_total * DISCOUNT_HIGH;
discounted_total = purchase_total - discount;
}

printf("Total purchases $%f\n", purchase_total);
printf("Teacher's discount (12%%) %fs\n", discount);
printf("Discounted total %f\n", discounted_total);
printf("Sales tax (5%%) %f\n", sales_tax);
printf("Total $%f\n", total);
}


/* calculation for nonteacher */
if (teacher =="n");
{
/* calculate only 5% sales tax */
sales_tax = purchase_total * sales_tax;
total = purchase_total + sales_tax;

printf("Total purchases $%f\n", purchase_total);
printf("Sales tax (5%%) %f\n", sales_tax);
printf("Total $%f\n", total);
}

return (0);
}

最佳答案

您在 if 之后有 ; 导致了问题

if (teacher == "y");
{

应该是

if (teacher == 'y')
{

还有

if (teacher =="n");

应该是

if (teacher == 'n')

还有一点:

scanf("%d", &teacher);

应该是

scanf("%c", &teacher);

然后注意将 == "n" 更改为 == 'n'

关于c - 我正在创建一个简单的程序来计算涉及 if/else 的销售总额并不断收到错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17329348/

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