来自' char *'的转换无效to' char [-fpermissive]

时间:2015-09-19 08:50:18

标签: c function

我是C..so的新手,请耐心等待..我想在函数calculatePrice(char)中调用函数printReceipt(char,char,char),但它说有' s在printReceipt的函数调用中,从char到char的无效转换错误。有人可以帮帮我吗?

double calculatePrice(char cust)        //calculate total price for each customer and return the value
{
    char code[5], size, top;
    double discount, drPrice, topPrice, price;


    printf("\n\t\t\t\tEnter the order's code :");
    scanf("%s", code);

    printf("\n\t\t\t\tEnter size of cup : ");
    scanf(" %c", &size);

    printf("\n\t\t\t\tEnter topping : ");
    scanf(" %c", &top);



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

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

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

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

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

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

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

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

    if(top == 'X')      //if no topping is selected
        topPrice = 0.00;

    if(cust == 'M')     //customer type
        price = 0.8 * (topPrice + drPrice);         

    else if(cust == 'S')    //customer type
        price = 0.85 * (topPrice + drPrice);    

    else if(cust == 'R')    //customer type
        price = topPrice + drPrice; 

    printReceipt(code, size, top);

    return price; //return value
}


void printReceipt(char code, char size, char top)
{
    FILE *f = fopen("receipt.txt", "w");

    if(f == NULL)
    {
        printf("Error opening file!\n");
        exit(1);
    }

    fprintf(f, "bla bla bla");


    fclose(f);
}

2 个答案:

答案 0 :(得分:1)

 void printReceipt(char code, char size, char top)

此函数需要char作为第一个参数,并在通话中传递char array -

  printReceipt(code, size, top);
                ^ code is char array  (declared as char code[5])

在函数char *中将第一个参数的类型更改为printReceipt或将单个字符传递给它。

根据您的需要进行更改。

为安全起见,请写下这个 - scanf("%s", code); -

 scanf("%4s", code);

答案 1 :(得分:1)

更改此

void printReceipt(char code, char size, char top) 

void printReceipt(char *code, char size, char top)