【发布时间】:2014-10-17 14:00:16
【问题描述】:
这是一个收银员程序。请先看截图。
循环是我认为问题所在。更有可能来自带有'j'和'k'的行。用户输入项目代码(i),例如2,根据文本文件搜索价格[2]=2.49,将价格保存到另一个数组ipprice[j],如果这是输入的第一个项目代码 ipprice[0],所以稍后我可以打印购买的第一个项目是 ipprice[0],第二个等等,那些 ipprice[j] 应该保持不变,但它没有,检查截屏。系统 cls 使代码看起来很乱,重复打印基本上只是试图在屏幕上保留一些内容,同时清除不需要的部分。使用 k 是因为 j 涉及常量 j++,每输入一个商品代码就递增,所以程序知道这是第一个购买的商品,第二个以此类推。
完整代码
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i,j=0,code,amt, key,lines=0;
int id[i],stock[i],k=0;
char name[i][20],product[100];
float price[i],sum;
float total=0;
char ipname[j][20],ch;
int quantity[j];
float ipprice[j];
float ipsub[j];
FILE*fp1;
fp1=fopen("Fruit.txt","r");
if(fp1==NULL)
{
printf("ERROR in opening file\n");
return 1;
}
else
{
while((ch=getc(fp1))!=EOF)
{
if(ch=='\n')
lines++;
}
}
fclose(fp1);
fp1=fopen("Fruit.txt","r");
if(fp1==NULL){
printf("ERROR in opening file\n");
return 1;
}
else
{
for(i=0;i<lines;i++){
fgets(product,sizeof(product),fp1);
id[i]=atoi(strtok(product,","));
strcpy(name[i],strtok(NULL,","));
price[i]=atof(strtok(NULL,","));
stock[i]=atoi(strtok(NULL,"\n"));
}
}
fclose(fp1);
printf("=============================================================\n");
for(i=0;i<lines;i++)
{
printf("%d:%-10s\t",i+1,name[i]);
}
printf("\n\n%-5s%-20s%-15s%-10s%s\n","No.","Product","Price","Quantity","Subtotal");
printf("%-5s%-20s%-15s%-10s%s\n","===","=======","=====","========","========");
do{
k=0;
if(j>0)
{
system("cls");
}
if(j>0)
{
printf("=============================================================\n");
for(i=0;i<lines;i++)
{
printf("%d:%-10s\t",i+1,name[i]);
}
printf("\n\n%-5s%-20s%-15s%-10s%s\n","No.","Product","Price","Quantity","Subtotal");
printf("%-5s%-20s%-15s%-10s%s\n","===","=======","=====","========","========");
for(k=0;k<j;k++)
{
printf("%-5d%-20s%-15.2f%-10d%.2f\n",
k+1,ipname[k],ipprice[k],quantity[k],ipsub[k]);
}
}
sum=0;
amt=0;
printf("\nProduct code:");
scanf("%d",&code);
i=code-1;
printf("Quantity:");
scanf("%d",&amt);
system("cls");
sum=price[i]*amt;
total=total+sum;
ipprice[j]=price[i];
strcpy(ipname[j],name[i]);
quantity[j]=amt;
ipsub[j]=sum;
printf("=============================================================\n");
for(i=0;i<lines;i++){
printf("%d:%-10s\t",i+1,name[i]);
}
printf("\n\n%-5s%-20s%-15s%-10s%s\n","No."
,"Product","Price","Quantity","Subtotal");
printf("%-5s%-20s%-15s%-10s%s\n","===","=======","=====","========","========");
for(k=0;k<=j;k++)
{
printf("%-5d%-20s%-15.2f%-10d%.2f\n",
k+1,ipname[k],ipprice[k],quantity[k],ipsub[k]);
}
printf("%d",j);
printf("\nPress ESC on keyboard to finalize the bill or Press any key continue...\n");
key=_getch();
if(key!=27)
{
j++;
}
}while(key!=27);
printf("%.2f",total);
return 0;
}
文本文件内容
18156,Apple,1.49,73
45776,Aprikot,1.59,23
73191,Avocado,2.49,63
72829,Banana,2.99,27
74084,Blueberry,5.49,36
79800,Coconut,3.49,80
16611,Grape,8.99,42
62690,Grapefruit,2.19,34
47089,Guava,4.99,42
70412,Jackfruit,19.99,29
44640,Kiwifruit,2.15,38
94768,Lemon,0.99,42
86240,Mango,3.99,62
78697,Orange,1.69,23
74470,Papaya,4.49,23
23959,Pear,2.49,36
78862,Pineapple,2.99,71
82943,Pomegranate,4.35,19
56180,Pomelo,12.99,72
67712,Starfruit,2.79,59
33974,Strawberry,13.99,31
12354,dildo,29.99,4
45584,Watermelon,6.88,21
循环
do{
k=0;
if(j>0)//because this is only necessary if it is second loop
{
system("cls");
}
if(j>0)//same reason, j is later used to display and save item purchased
{
printf("=============================================================\n");
for(i=0;i<lines;i++)
{
printf("%d:%-10s\t",i+1,name[i]);
}
printf("\n\n%-5s%-20s%-15s%-10s%s\n","No.","Product","Price","Quantity","Subtotal");
printf("%-5s%-20s%-15s%-10s%s\n","===","=======","=====","========","========");
for(k=0;k<j;k++)//i am trying to keep these content on screen while system cls
{
printf("%-5d%-20s%-15.2f%-10d%.2f\n",
k+1,ipname[k],ipprice[k],quantity[k],ipsub[k]);
}
}
sum=0;
amt=0;
printf("\nProduct code:");
scanf("%d",&code);
i=code-1;
printf("Quantity:");
scanf("%d",&amt);//amt amount
system("cls");
sum=price[i]*amt;
total=total+sum;
ipprice[j]=price[i]; //ip is itempurchased
strcpy(ipname[j],name[i]);
quantity[j]=amt;
ipsub[j]=sum;//subtotal
printf("=============================================================\n");
for(i=0;i<lines;i++){
printf("%d:%-10s\t",i+1,name[i]);
}
printf("\n\n%-5s%-20s%-15s%-10s%s\n",
"No.","Product","Price","Quantity","Subtotal");
printf("%-5s%-20s%-15s%-10s%s\n","===","=======","=====","========","========");
for(k=0;k<=j;k++)
{
printf("%-5d%-20s%-15.2f%-10d%.2f\n",
k+1,ipname[k],ipprice[k],quantity[k],ipsub[k]);
}
printf("\nPress ESC on keyboard to finalize the bill or Press any key continue...\n");
key=_getch();
if(key!=27)
{
j++;
}
} while(key!=27);
【问题讨论】:
-
感谢编辑
-
您的代码以
int i, ...;然后int id[i],stock[i],k=0;打开您认为i在id和stock的维度声明中是什么? IE。前两行调用未定义的行为。该问题在main()的声明中重复出现:例如:char name[i][20]、float price[i]等。同样将j作为大小索引(至少初始化为0,但它根本没有帮助)。在那之后我停止了阅读。 -
@WhozCraig 嗨,谢谢你的回答,你能给我一个正确方法的例子吗? i 用于存储文本文件中的产品信息,如果文本文件有 23 行,则最大 i 为 22,我可能不清楚数组,你能告诉我正确的方法吗?
-
@problematic 不是帖子的核心问题,但建议使用
int ch;而不是char ch;。 -
您需要编译所有警告和调试信息,并使用调试器(必备技能)。