这是一个收银员计划。请先看截图。
循环是我认为问题来自的地方。更有可能来自'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);
答案 0 :(得分:0)
有效的新代码:
#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[100],stock[100],k=0;
char name[100][20],product[100];
float price[100],sum;
float total=0;
char ipname[100][20];
int quantity[100], ch;
float ipprice[100];
float ipsub[100];
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");
scanf(" %c", &key);
if(key!=27)
{
j++;
}
}while(key!=27);
printf("%d",j);
printf("\n%.2f\n",total);
return 0;
}
=============================================== ====
这应该是这样的: http://i30.photobucket.com/albums/c323/dumfuq/Screenshot37_zps81596e1a.png
最后一个数字'11'就是我试图监控j ++正确运行。