首页 > 解决方案 > 如何打印输入的项目名称?

问题描述

我是编程新手,所以我很难弄清楚如何输出循环中输入的项目的名称,所有其他计算都在执行,但是当输出项目的名称时我不断得到未知值。

#include <stdio.h>
int main()
 {
int count,num,a;
char *item [15][100];
float price,net_total, tax, total_pay,grandT;

do{
printf("Please enter the name of the item:");scanf("%s",&item[a]);
printf("\n");
printf("Please enter the price of the item: $");scanf("%f",&price);
        
net_total+=price;
tax=0.14*net_total;
grandT=net_total+tax;

printf("\nSelect '1' to continue or '0' to exit:\n");
scanf("%d",&num);
count++;
}
while (num!=0);
system ("cls");
printf("\tSummary Information of Items purchsed\n");
printf("Name(s)of the item(s) purchased: \t%s\n ",item[a]); 
printf("Net total:\t$%.2f\n",net_total);
printf("Total tax:\t$%.2f\n",tax);
printf("Total payable:\t$%.2f\n ",grandT);

    
}`

标签: c

解决方案


1/ 你应该缩进你的代码,这样更容易阅读。
2/ 你没有初始化a,countnet_total. (a是从0到的索引15)。
3/char item [15][100]

    #include <stdio.h>
    
    int main()
    {
      int count=0,num,a=0;
      char item [15][100];
      float price,net_total=0, tax, total_pay,grandT;
    
      do
        {
          printf("Please enter the name of the item:");
          scanf("%s",&item[a]);
          printf("\n");
          printf("Please enter the price of the item: $");
          scanf("%f",&price);
          net_total+=price;
          tax=0.14*net_total;
          grandT=net_total+tax;
          
          printf("\nSelect '1' to continue or '0' to exit:\n");
          scanf("%d",&num);
          count++;
    
     
        }
      while (num!=0);
      system ("cls");
      printf("\tSummary Information of Items purchsed\n");
      printf("Name(s)of the item(s) purchased: \t%s\n ",item[a]); 
      printf("Net total:\t$%.2f\n",net_total);
      printf("Total tax:\t$%.2f\n",tax);
      printf("Total payable:\t$%.2f\n ",grandT);
   }

推荐阅读