这个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);
}
}
发布于 2017-02-25 18:25:39
您的代码有很多语法错误。我刚修好了。这是它的工作代码。将此代码与前面的代码进行比较,以了解您的错误。
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;
}
发布于 2017-02-25 17:57:17
你还没有展示完整的代码。
问:x
是否被声明为整数(或者更好,unsigned
)?
问:启动循环时,你确定是x > 0
吗?
..。还有..。
如果在Windows中以CMD提示符的形式运行程序.确保在程序结束前添加getchar()
。否则,程序将退出,您的窗口可能会消失之前,您看到任何输出。
此外:
注意天气叶片对您的scanf
格式错误的建议。以下是两个很好的链接:
希望能帮上忙
https://stackoverflow.com/questions/42459541
复制相似问题