0

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

【警告】指针与整数的比较【默认开启】

#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);
}
4

3 回答 3

4

你有;之后if导致问题

if (teacher == "y");
{

应该

if (teacher == 'y')
{

if (teacher =="n");

应该

if (teacher == 'n')

还有一件事:

scanf("%d", &teacher);

应该

scanf("%c", &teacher);

然后注意== "n"to的变化== 'n'

于 2013-06-26T20:00:38.967 回答
0

这是导致您的警告的原因:

if (teacher == "y")

teacher是一个int-"y"是一个字符串。你无法比较它们。

您的代码中还有许多其他问题,包括当您teacher首先进入时,您要求一个字符,但扫描一个int.

于 2013-06-26T20:04:29.653 回答
0

改变

if (teacher == "y");

if (teacher == 'y');

和改变

if (teacher == "n");

if (teacher == 'n');

“n”或“y”将是一个字符数组(C 中的字符串)。因此它被视为一个指针,这就是为什么你会得到你得到的错误。

此外,当我运行您的测试程序时,与“y”和“n”逻辑的比较有效,但我只是得到了全面的 0。你需要修正一些逻辑。我会把它留给你。

于 2013-06-26T20:04:47.833 回答