我的代码是:
#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预处理器指令,我想在程序中测试它。我创建了一个定义了一些东西的程序。只需1个员工条目就可以正常工作,但是当我尝试将其扩展为一组员工时,我会遇到编译器错误。
具体来说是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
答案 0 :(得分:2)
该行以sales * commission_rate
结尾。我想你打算写sales[i] * commission_rate
。
答案 1 :(得分:1)
commission[i] = (sales[i] * commission_rate) > maxcommission? 5000 : sales * commission_rate; // this line gives me an error`
应该是
commission[i] = (sales[i] * commission_rate) > maxcommission? 5000 : sales[i] * commission_rate; // this line gives me an error`
答案 2 :(得分:0)
指令适用于数组。
: sales * commission_rate;
您正在尝试将指针(不取消引用它)乘以常量。这是错误。