New code that works:
#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;
}
===================================================
This is how it is supposed to look like:
http://i30.photobucket.com/albums/c323/dumfuq/Screenshot37_zps81596e1a.png
The last number ’11’ is just me trying to monitor the j++ is going correctly.
1
solved Program gives unstable output? [closed]