C 未计算金额
这个C程序是用来打印发票的,唯一的问题是它不会输出单价和计算金额。有人能告诉我怎么做吗。这是完整的代码。如果有任何其他错误,请帮助C 未计算金额,c,C,这个C程序是用来打印发票的,唯一的问题是它不会输出单价和计算金额。有人能告诉我怎么做吗。这是完整的代码。如果有任何其他错误,请帮助 struct PRODUCTSINFO { int code; // products number int qty; char name[ 50 ]; // products name double unit_price; // account unit_price }; // end structure PRODUCTSINFO struct PRODU
struct PRODUCTSINFO {
int code; // products number
int qty;
char name[ 50 ]; // products name
double unit_price; // account unit_price
}; // end structure PRODUCTSINFO
struct PRODUCTSINFO products[100] ;
int main(){
int p;
int x ;
int i=0;
double amount=0;
printf("Enter the amount of products to be purchased : ");
scanf("%d",&x);
for(i=0;i<x;i++){
printf("\nEnter product code #%d : ",i+1);
scanf(" %d",&products[i].code);
printf("\nEnter product name#%d:",i+1);
scanf("%s",&products[i].name);
printf("\nPlease quantity#%d : ",i+1);
scanf("%d",&products[i].qty);
printf("Enter unit price#%d:",i+1);
scanf("%.lf",&products[i].unit_price);
fflush(stdin);
}
system("cls");
printf("************************INVOICE*******************************\n");
printf("-----------------------------------------------\n);
printf("S/N | CODE | NAME OF PRODUCTS | QUANTITY | UNIT PRICE |AMOUNT \n");
printf("------------------------------------------------------\n");
for(i=0;i<x;i++){
printf("\n%d",i);
printf("\t %d",products[p].code);
printf("\t %s",products[p].name);
printf("\t\t\t%d",products[p].qty);
printf("\t\t%.2f",products[p].unit_price);
p++;
amount=products[p].qty*products[p].unit_price;
printf("\t%.2f\n",amount);
}
}
struct PRODUCTSINFO{
int code;//产品编号
整数数量;
字符名称[50];//产品名称
双倍单价;//科目单价
}; // 终端结构产品信息
结构产品信息产品[100];
int main(){
INTP;
int x;
int i=0;
双倍金额=0;
printf(“输入要购买的产品数量:”);
scanf(“%d”和&x);
对于(i=0;i您尚未显示完整的代码
问:x
是否声明为整数(或者更好,不带符号的)
问:你确定启动循环时x>0
……还有
如果您在Windows中以CMD提示符运行程序…请确保在程序结束之前添加getchar()
。否则,程序将退出,并且您的窗口可能在看到任何输出之前消失
此外:
请注意Weather Vane关于您的scanf
格式错误的建议。这里有两个很好的链接:
'希望这有帮助。您的代码有很多语法错误。我刚刚修复了它。下面是它的工作代码。
将此代码与前一代码进行比较,以了解您的错误
struct PRODUCTSINFO {
int code; // products number
int qty;
char name[ 50 ]; // products name
double unit_price; // account unit_price
}; // end structure PRODUCTSINFO
struct PRODUCTSINFO products[100] ;
int main(){
int p;
int x ;
int i=0;
double amount=0;
int total = 0;
printf("Enter the amount of products to be purchased : ");
scanf("%d",&x);
for(i=0;i<x;i++){
printf("\nEnter product code #%d : ",i+1);
scanf(" %d",&products[i].code);
printf("\nEnter product name#%d:",i+1);
scanf("%s",products[i].name);
printf("\nPlease quantity#%d : ",i+1);
scanf("%d",&products[i].qty);
printf("Enter unit price#%d:",i+1);
scanf("%lf",&products[i].unit_price);
}
printf("************************INVOICE*******************************\n");
printf("-----------------------------------------------\n");
printf("S/N | CODE | NAME OF PRODUCTS | QUANTITY | UNIT PRICE |AMOUNT \n");
printf("------------------------------------------------------\n");
for(i=0;i<x;i++){
printf("\n%d",i);
printf("\t %d",products[i].code);
printf("\t %s",products[i].name);
printf("\t\t\t%d",products[i].qty);
printf("\t\t%.2f",products[i].unit_price);
p++;
amount=products[i].qty*products[i].unit_price;
total += amount;
printf("\t%.2f\n",amount);
}
Printf("%d",total);
return 0;
}
struct PRODUCTSINFO{
int code;//产品编号
整数数量;
字符名称[50];//产品名称
双倍单价;//科目单价
};//结束结构PRODUCTSINFO
结构产品信息产品[100];
int main(){
INTP;
int x;
int i=0;
双倍金额=0;
int-total=0;
printf(“输入要购买的产品数量:”);
scanf(“%d”和&x);
对于(i=0;iInscanf(%.lf)和&product[i].单价)
请删除“%.lf”
中的点。它应该是“%lf”
。另外,产品名称条目不应该包含和。C规范明确指出,在仅输入的流上调用fflush
(这是stdin
)是未定义的行为。某些实现允许它作为C语言的扩展,但您通常不应该这样做。我使用它是因为它跳过了一行。我将如何计算总数?它将是总数+=金额;我将如何计算总数?它将是总数+=金额;是的,最后打印它。您可以编辑它吗因为它不计算总数,所以只计算一个金额,这是最后一个金额