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 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/

相关文章:

java - 编程新手 : What is the difference between run time error and compilation error?

c++ - 为什么我们在当前文件中看不到#include 粘贴的文本?

c - for 循环条件下的 getchar()

c - 在 C 中从 shell 声明全局 CHAR 数组

c - 同一台机器上的进程间通信,信号还是套接字,如何决定?

c - "assignment discards ' const 非常量指针上的 ' qualifier"错误

c - 我怎样才能成功地在文件 IO 中使用删除功能 -- C

c - 了解 double 和整数

c - 完全数逻辑检查

c - 用 C/gcc 内在函数 : no intrinsic for VSWP? 交换 NEON vector 的一半