c - 如何打印输入的项目名称?
问题描述
我是编程新手,所以我很难弄清楚如何输出循环中输入的项目的名称,所有其他计算都在执行,但是当输出项目的名称时我不断得到未知值。
#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);
}`
解决方案
1/ 你应该缩进你的代码,这样更容易阅读。
2/ 你没有初始化a
,count
和net_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);
}
推荐阅读
- python - 如何将多个但特定的行从一个数据帧添加到另一个数据帧
- javascript - PrimeFaces 渲染的所有 JavaScript 是什么?
- javascript - Angular i18n 本地化不使用管道转换插值
- php - 如何使用 PHPUnit 测试带参数的 __construct?
- mongodb - 如何将现有的 mongodb 数据库集成到当前项目?
- gcc - 无法编译任何 c 程序(/usr/bin/ld: 1: 语法错误:意外换行)
- arrays - DSA 问题:从包含相同关联值的数组中删除重复项
- python - 使用名称创建异步锁
- python - Python Zip 函数的时间复杂度
- amazon-s3 - 托管在多个域使用的 Cloudfront 后面的 S3 上的资产,访问控制允许来源没有变化