Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5年前关闭。
这是一个收银员程序。请先查看屏幕截图。
循环是我认为问题出处。用户输入项目代码(i),例如2,根据文本文件搜索价格[2] = 2.49,然后将价格保存到另一个数组ipprice [j]中,如果这是输入的第一个商品代码ipprice [0],因此以后我可以打印购买的第一个商品是ipprice [0],第二个,依此类推,这些ipprice [j]预计保持不变,但是没有,请检查屏幕截图。系统cls使代码看起来混乱,重复打印基本上只是在清除不需要的部分的同时尝试在屏幕上保留一些内容。使用k是因为j涉及常数j ++,对于输入的每个商品代码,其递增,因此程序知道这是所购买的第一件商品,以此类推。
完整代码
文字档内容
循环
================================================== =
它应该是这样的:
http://i30.photobucket.com/albums/c323/dumfuq/Screenshot37_zps81596e1a.png
最后一个数字“ 11”只是我试图监视j ++正常运行的原因。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5年前关闭。
这是一个收银员程序。请先查看屏幕截图。
循环是我认为问题出处。用户输入项目代码(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);
最佳答案
有效的新代码:
#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 ++正常运行的原因。
10-08 01:55