我的代码是:

#include <stdio.h>
#include <ctype.h>
#define maxcommission 5000
#define commission_rate .1

main()
{
    char   employee[100][65], response[10];
    double sales[100], commission[100];
    int    i;
    for(i=0;;)
    {
        printf("Do you want to add another employee? ");
        fgets(response, 10, stdin);
        response[(strlen(response)-1)]='\0';
        if (strcmp(response[i], "no") == 0 || strcmp(response[i], "n") ==0 )
            break;

        printf("Who is the first employee? ");
        fgets(employee[i], 65, stdin);
        employee[i][(strlen(employee[i])-1)]='\0';
        printf("How much did you sell?\n");
        scanf("%lf", &sales[i]);
        fflush(stdin);
        commission[i] = (sales[i] * commission_rate) > maxcommission? 5000 : sales * commission_rate; // this line gives me an error
    }
}

我刚刚学习了define pre-processor指令,我想在程序中测试它。我已经创建了一个程序来定义一些东西。它只需要一个employee条目就可以工作,但是当我试图将它扩展成一个employees数组时,我会得到编译器错误。
特别是invalid operands to binary * (have 'double *' and 'double')。为什么这会给我一个错误,当一个单一的条目可以工作?我要怎么解决这个问题?
这是问题所在:
commission[i] = (sales[i] * commission_rate) > maxcommission? 5000 : sales * commission_rate; // this line gives me an error

最佳答案

该行以sales * commission_rate结束。我想你是想写sales[i] * commission_rate

10-06 09:44