问题描述
我需要有关代码的帮助。我的代码是 C 源代码格式,但它不能在 Dev C++ 软件上运行。我和我的队友检查了几次,我的代码没有错误,但它无法运行。有人,请帮助我。谢谢。
#include<stdio.h>
#include<stdlib.h>
#define TAX 0.08
void getInput(struct order myOrder[],int order);
void processOrder(struct order myOrder[],int order);
struct order
{
char productID[6];
float unitPrice,grosstotal,netTotal,serviceTax;
int qtyOrdered;
};
main()
{
struct order myOrder[50];
int order;
printf("ORDER PROCESSING\n");
printf("=================\n");
printf("How many orders today ? ");
scanf("%d",&order);
getInput(myOrder,order);
processOrder(myOrder,order);
system("PAUSE");
}
void getInput(struct order myOrder[],int order)
{
for (int i = 0;i < order;i++)
{
printf("Enter productID :");
scanf("%s",&myOrder[i].productID);
printf("Enter unit price : RM ");
scanf("%f",&myOrder[i].unitPrice);
printf("Enter quantity ordered : ");
scanf("%d",&myOrder[i].qtyOrdered);
printf("\n");
}
}
void processOrder(struct order myOrder[],int order)
{
float sum = 0.0;
for (int i = 0;i < order;i++)
{
printf("Order : %s\n",myOrder[i].productID);
printf("Unit Price : RM %.2f\n",myOrder[i].unitPrice);
printf("Qty Ordered : %d\n",myOrder[i].qtyOrdered);
myOrder[i].grosstotal = myOrder[i].unitPrice * myOrder[i].qtyOrdered;
printf("Gross Revenue : RM %.2f\n",myOrder[i].grosstotal);
myOrder[i].serviceTax = myOrder[i].grosstotal * TAX;
printf("Service Tax : RM %.2f\n",myOrder[i].serviceTax);
myOrder[i].netTotal = myOrder[i].grosstotal - myOrder[i].serviceTax;
printf(": : Net Revenue : RM%.2f\n\n",myOrder[i].netTotal);
sum += myOrder[i].netTotal;
}
printf("* TOTAL INCOME EARN :: RM%.2f",sum);
}
解决方法
struct order
的声明应该在使用它之前。
错误:
void getInput(struct order myOrder[],int order);
void processOrder(struct order myOrder[],int order);
struct order
{
char productID[6];
float unitPrice,grossTotal,netTotal,serviceTax;
int qtyOrdered;
};
正确:
struct order
{
char productID[6];
float unitPrice,serviceTax;
int qtyOrdered;
};
void getInput(struct order myOrder[],int order);
还有 scanf("%s",&myOrder[i].productID);
是错误的。 char*
(指向字符的指针)应该传递给 %s
,而不是 char(*)[6]
(指向整个数组的指针)。应删除 &
。