I m trying to read structure from the file.But it only reads till the 25th line after that it enters the loop but does not read..what's wrong ?please help... asap
This is the code:
#include <iostream>
#include <fstream>
#include <cstring>
#include <conio.h>
#include <iomanip.h>
using namespace std;
struct product {
int no;
char name[80];
float price[5];
int page;
}acc[1300];
void edit();
void add();
void addon();
void search();
int main()
{
int i,n,ch;
cout<<"\t\t\t\t"<<"******Welcome*******"<<endl<<endl<<endl;
//add();
// now, read back;
ifstream inbal("balance");
if(!inbal) {
cout << "Cannot open file.\n";
return 1;
}
for(i=1;i<=1300;i++)
{
inbal.read((char *) &acc[i], sizeof(struct product));
}
inbal.close();
//PRINT THE LIST......
int count=0;
for(i=1;i<30;i++)
{
int j;
while(count==0)
{
cout<<setw(5)<<left<<"NO."<<setw(40)<<left<<"NAME"<<setw(5)<<left<<"PAGE"<<setw(5)<<left<<"B(NABH)"<<setw(5)<<left<<"B(NON)"<<setw(5)<<left<<"J(NABH)"<<setw(5)<<left<<"J(NON)"<<endl;
count++;
}
cout<<setw(5)<<left<<acc[i].no<<setw(40)<<left<<acc[i].name;
cout<<setw(5)<<left<<acc[i].page;
for(j=0;j<4;j++)
cout<<setw(7)<<left<<acc[i].price[j];
cout<<endl;
}
while(1)
{
cout<<endl<<"Press 1 to search, 2 to edit, 3 to add to list and any other key to exit"<<endl;
cin>>ch;
switch (ch)
{
case 1:
search();
break;
case 2:
edit();
break;
case 3:
addon();
break;
default:
break;
}
if(ch!=1 && ch!=2 && ch!=3)
break;
}
getch ();
}
void add()
{
int i,j;
char place[4][80]={"B(NABH)","B(NON)","J(NABH)","J(NON)"};
for(i=1;i<2;i++)
{
cout<<"Enter Number:"<<endl;
cin>>acc[i].no;
cout<<"Enter Name:"<<endl;
cin>>acc[i].name;
cout<<"Enter Page:"<<endl;
cin>>acc[i].page;
cout<<"Enter price in:"<<endl;
for(j=0;j<4;j++)
{
cout<<place[j]<<endl;
cin>>acc[i].price[j];
cout<<endl;
}
}
// write data
ofstream outbal("balance");
if(!outbal) {
cout << "Cannot open file.\n";
}
for(i=1;i<2;i++)
{
outbal.write((char *) &acc[i], sizeof(struct product));
}
outbal.close();
}
void search()
{
int n=0,i=1,j,count=0,len,len1;
char place[4][80]={"B(NABH)","B(NON)","J(NABH)","J(NON)"};
char nam[80];
while(acc[i].page!=0)
i++;
n=i+1;
cout<<"Enter the name to be searched:"<<endl;
cin>>nam;
for(i=1;i<=n;i++)
{
int k=0;
len=strlen(acc[i].name);
for(j=0;j<len;j++)
{
if(nam[k]==acc[i].name[j])
{
k++;
}
}
len1=strlen(nam);
if(k==len1)
{
while(count==0)
{
cout<<setw(5)<<left<<"NO."<<setw(40)<<left<<"NAME"<<setw(5)<<left<<"PAGE"<<setw(5)<<left<<"B(NABH)"<<setw(5)<<left<<"B(NON)"<<setw(5)<<left<<"J(NABH)"<<setw(5)<<left<<"J(NON)"<<endl;
count++;
}
cout<<setw(5)<<left<<acc[i].no<<setw(40)<<left<<acc[i].name;
cout<<setw(5)<<left<<acc[i].page;
for(j=0;j<4;j++)
cout<<setw(7)<<left<<acc[i].price[j];
cout<<endl;
}
}
}
void edit()
{
int i,j,no,n;
char place[4][80]={"B(NABH)","B(NON)","J(NABH)","J(NON)"};
cout<<"Enter the number of the element to edit:"<<endl;
cin>>i;
acc[i].no=i;
cout<<"Enter New Name:"<<endl;
cin>>acc[i].name;
cout<<"Enter New Page:"<<endl;
cin>>acc[i].page;
cout<<"Enter New price in:"<<endl;
for(j=0;j<4;j++)
{
cout<<place[j]<<endl;
cin>>acc[i].price[j];
cout<<endl;