这个C程序是打印发票的,唯一的问题是不会输出单价和计算金额。谁能告诉我怎么做。这是现在的完整代码。如果还有其他错误,请帮助。
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);
}
}
最佳答案
您没有显示完整的代码。
问:x
是否声明为整数(或更好的unsigned
)?
问:当你开始循环时,你确定 x > 0
吗?
...和...
如果您在 Windows 中将程序作为 CMD 提示符运行...请务必在程序结束前添加 getchar()
。否则,程序将退出并且您的窗口可能会在您看到任何输出之前消失。
还有:
请注意 Weather Vane 关于您的 scanf
格式错误的建议。这里有两个很好的链接:
'希望对你有帮助
关于c - 金额计算失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42459541/