函数calculatePrice的类型错误冲突

时间:2015-09-16 15:43:48

标签: c++ c function

我编写了这个编码,然后它说'ErrorPrice'的[Error]冲突类型存在错误。我不知道为什么它似乎是一个错误。任何人都可以帮我纠正这个错误? :((

void Add(struct data* m[])//, struct data* s)
{
    char cust, choice1, choice2;
    int i, counter = 0;
    double totPrice = 0.00;


    do
    {           
        printf("Member(M) / Student(S) / Regular Customer(R)?");
        scanf("%c", &cust);

        printf("Enter name: ");
        scanf("%s", &m[counter]->Mname);

        printf("Enter phone number : ");
        scanf("%s", &m[counter]->MphoneNum);

        printf("Enter id : ");
        scanf("%s", &m[counter]->Mid);

        printf("Enter address");
        scanf("%s", &m[counter]->Maddress);

        printf("Enter e-mail : ");
        scanf("%s", &m[counter]->mail);

        do
        {
        /*  totPrice +=*/ calculatePrice(cust);

            printf("Wanna add another order?");
            scanf("%s", choice1);
        }           
        while(choice1 == 'Y');


        counter++;
        m[counter]->payment = totPrice;

        printf("Wanna add another customer?");
        scanf("%s", choice2);
    }

    while(choice2 == 'Y');  
}

double calculatePrice(char cust)
{
    char code[4], size, top;
    double discount, drPrice, topPrice, price;

    if(cust == 'M')
    //searchMember(&m, &s); 
        discount = 0.8;         

    else if(cust == 'S')
        discount = 0.85;

    else if(cust == 'R')
        discount = 0.0;


    printf("Enter the order's code :");
    scanf("%s", code);

    printf("Enter size of cup : ");
    scanf("%s", size);

    printf("Enter topping : ");
    scanf("%s", top);

    if((strcmp(code, "T1")) || (strcmp(code, "T2")) || (strcmp(code, "T3")) ||(strcmp(code, "T4")) || (strcmp(code, "T5")) == 0)
    {
        if(size == 'R')
            drPrice = 4.90;
        else
            drPrice = 5.90;
    }

    else if((strcmp(code, "CH1")) || (strcmp(code, "CH2")) || (strcmp(code, "CH3")) || (strcmp(code, "CH4")) || (strcmp(code, "CH5")) == 0)
    {
        if(size == 'R')
            drPrice = 6.90;
        else
            drPrice = 7.90;
    }

    else if((strcmp(code, "C2")) || (strcmp(code, "C3")) || (strcmp(code, "C5")) == 0)
        drPrice = 5.90;     

    else if((strcmp(code, "S1")) || (strcmp(code, "S2")) || (strcmp(code, "S5")) || (strcmp(code, "S7")) || (strcmp(code, "S9")) == 0)
        drPrice = 6.90;

    else if((strcmp(code, "S3")) || (strcmp(code, "S4")) || (strcmp(code, "S6")) || (strcmp(code, "S8")) || (strcmp(code, "S10")) || (strcmp(code, "S11")) == 0)
        drPrice = 5.90; //check harga balik

    else if((strcmp(code, "C1")) == 0)
        drPrice = 3.90;

    else if((strcmp(code, "C4")) == 0)
        drPrice = 4.90;

    if(top == 'B' || top == 'G' || top == 'J' || top == 'P' || top == 'A')
        topPrice = 0.60;

    else if(top == 'X')
        topPrice = 0.00;

    price = discount * (topPrice + drPrice);

    return price; //return value
}

1 个答案:

答案 0 :(得分:0)

在该函数中calculatePrice

printf("Enter size of cup : ");
scanf("%s", size);

printf("Enter topping : ");
scanf("%s", top);

要求输入字符串,但目标类型为char

应该是

printf("Enter size of cup : ");
scanf(" %c", &size);

printf("Enter topping : ");
scanf(" %c", &top);

注意%c之前的空格,它会清除上一次输入后留下的空格。

另请注意,char code[4]只能接受3个字符。这是一种危险的做事方式,因为你不限制输入长度。