You are here:

C++/problem in c++ project

Advertisement


Question
i had prepared following project in c++.suppose if i enter 10 accounts than delete any account except 10th than display all accounts is giving undesired output.plz help me urgently as i hav to submit project soon..i use turbo c++ compiler on XP.

#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
  void process(void);
  void deposit(void);
  void addrecord();
  void delrecord();
  void modrecord();
  void disprecord();
  void dispall();
  void withdraw();
  float w,z;

class account
{
int accountno,cls;
char name[30],type;
float deposit,withdraw;
 public:
    account()          //constructor
   {
     deposit=withdraw=0;
   }

     int giveaccountno()
   {
      return accountno;
   }
    void getdata(int mrno)
   {
      accountno=mrno+1;
      cout<<"\n\tACCOUNT NUMBER        ::  ";
      cout<<accountno<<" ";
      cout<<"\n\tENTER YOUR NAME       ::  ";
      gets(name);
      cout<<" ";
      cout<<"\n\tENTER TYPE OF ACCOUNT SAVING(s)/CURRENT(c)  ::  ";
      cin>>type;
      cout<<" ";
      cout<<"\n\tENTER INITIAL AMOUNT  ::Rs  ";cin>>deposit;
      cout<<" ";
    }

  void withdrawal(int m)
  {
     cout<<"\n\tAMOUNT BEFORE WITHDRAWING::Rs  "<<deposit<<" ";
     deposit=deposit-m;
     cout<<"\n\tAMOUNT AFTER WITHDRAWING::Rs  "<<deposit;
   }

  void deposital(int m)
   {
     cout<<"\n\tAMOUNT BEFORE DEPOSIT::Rs  "<<deposit<<" ";
     deposit=deposit+m;
     cout<<"\n\tAMOUNT AFTER DEPOSIT ::Rs  "<<deposit;
    }

   void dispdata()
     {
      int scrnt=0;
      if(scrnt==1)
         {
        clrscr();
        cout<<"\n\tDISPLAY ALL MENU ";
        scrnt=0;
         }
       cout<<"\n\nACCOUNT NUMBER  ::";
       cout<<accountno;
       cout<<"\nNAME OF DEPOSITER   ::";
       cout<<name;
       cout<<"\nTYPE OF ACCOUNT SAVING(s)/CURRENT(c)  ::  ";
       cout<<type;
       cout<<"\nBALANCE  ::Rs.  ";
       cout<<deposit;
       scrnt++;
      }
};
//-------------starting of main--------------


void main()
{
int menuch;
process();
do
{
  clrscr();
  setbkcolor(BLACK);
  setcolor(CYAN);
  cout<<"\t\tMAIN MENU";
  cout<<"\n\t\t 1.NEW ACCOUNT     ";
  cout<<"\n\t\t 2.CLOSE AN ACCOUNT ";
  cout<<"\n\t\t 3.MODIFY AN ACCOUNT  ";
  cout<<"\n\t\t 4.DISPLAY AN ACCOUNT ";
  cout<<"\n\t\t 5.DISPLAY ALL RECORDS ";
  cout<<"\n\t\t 6.WITHDRAW AMOUNT";
  cout<<"\n\t\t 7.DEPOSIT AMOUNT";
  cout<<"\n\t\t 8.EXIT     ";
  cout<<"\n\n\t ENTER YOUR CHOICE  ";
  cin>>menuch;
  switch(menuch)
   {
     case 1:addrecord();break;
     case 2:delrecord();break;
     case 3:modrecord();break;
     case 4:disprecord();break;
     case 5:dispall();break;
     case 6:withdraw();break;
     case 7:deposit();break;
   }
} while(menuch!=8);

}


void addrecord()
{
account obj_1,obj_2;
fstream fout;
fout.open("banking.txt",ios::in|ios::binary);
/*if(!fout)
{
  cout<<"FILE OPEN ERROR  ";
  getch();
  return;
} */
  int recsize=sizeof(account);
  fout.seekg(0,ios::end);
  fout.seekg(-1*recsize,ios::cur|ios::binary);
  fout.read((char*)&obj_1,recsize);
  int mrno=obj_1.giveaccountno();
  fout.close();
  clrscr();
  cout<<"\nADD MENU";
  obj_2.getdata(mrno);
  fout.open("banking.txt",ios::app|ios::binary);
  if(!fout)
  {
    cout<<"FILE OPEN ERROR  ";
    getch();
    return;
  }
     fout.write((char*)&obj_2,recsize);
     cout<<"\n\tRECORD ADDED TO DATABASE"<<"\nPress any key to continue... ";
     getch();
     fout.close();
  }


void dispall()
{
account obj_3;
fstream fout;
int recsize=sizeof(account);
int countrec=0;
clrscr();
cout<<"\n\tDISPLAY ALL MENU ";
fout.open("banking.txt",ios::in|ios::binary);
if(!fout)
{
  cout<<"\nFILE OPEN ERROR  ";
  getch();
  return;
}
while(fout.read((char*)&obj_3,recsize))
{
  obj_3.dispdata();
  countrec++;
  cout<<"\n\tPRESS ANY KEY FOR NEXT....";
  getch();
}
clrscr();
cout<<"\n\tEND OF FILE.TOTAL NUMBER OF RECORDS..."<<countrec;
cout<<"\nPress any key......";
getch();
fout.close();
}


void disprecord()
{
 account obj_4;
 fstream fout;
 int mrno,flag=0;
 int recsize=sizeof(account);
 clrscr();
 cout<<"\n\tDISPLAY A RECORD MENU ";
 fout.open("banking.txt",ios::in|ios::binary);
 if(!fout)
 {
   cout<<"\nFILE OPEN ERROR  ";
  getch();
  return;
 }
 cout<<" \n\tENTER  THE ACCOUNT NUMBER  ";
 cin>>mrno;
 while(fout.read((char*)&obj_4,recsize))
  {
    if (obj_4.giveaccountno()==mrno)
     {
     obj_4.dispdata();
     cout<<"\n\tPress any key.....";
     flag=1;break;
     }
  }
 if(flag==0)
  {
  cout<<" \n\tNO SUCH ACCOUNT EXIST  ";
   cout<<" \n\tPress any key......";
  }
getch();
fout.close();
}


void delrecord()
{
  account obj_5;
  fstream fout,temp;
  int mrno,flag;
  int recsize=sizeof(account);
  clrscr();
  cout<<"\n\t CLOSE ACCOUNT MENU ";
  fout.open("banking.txt",ios::in|ios::binary);
if(!fout)
{
  cout<<"\n\tFILE OPEN ERROR  ";
  getch();
  return;
}
 temp.open("temp.txt",ios::app|ios::binary);
 if(!temp)
  {
   cout<<"\n\tFILE OPEN ERROR  ";
  getch();
   return;
  }
cout<<" \n\tENTER THE ACCOUNT NUMBER ";
cin>>mrno;
while(fout.read((char*)&obj_5,recsize))
  {
  if(obj_5.giveaccountno()==mrno)
     {
     obj_5.dispdata();
     char confirm;
      cout<<"\n\tARE YOU SURE TO DELETE IT(Y/N)..";cin>>confirm;
        if(confirm=='Y'||confirm=='y')
        {
        fout.read((char*)&obj_5,recsize);
        cout<<"\n\tRECORD DELETED FORM DATABASE";
        cout<<"\n\tpress any key....";
         flag=1;
        if(!fout)
        break;
        }
     flag=1;
     }
  temp.write((char*)&obj_5,recsize);}
  fout.close();
   temp.close();
  remove("banking.txt");
  rename("temp.txt","banking.txt");
  if(flag==0)
     {
       cout<<"\n\tNO SUCH ACCOUNT EXIST";
       cout<<"\n\tPress any key.....";
     }
    getch();
}



void modrecord()
{
  account obj_6;
  fstream fout;
  int mrno,flag=0;
  int recsize=sizeof(account);
   clrscr();
   cout<<"\n\tMODIFY RECORD MENU ";
   fout.open("banking.txt",ios::in|ios::out|ios::binary);
   if(!fout)
  {
     cout<<"FILE OPEN ERROR  ";
     getch();
     return;
  }
   fout.seekg(ios::beg);
  cout<<"\n\tENTER RECORD NUMBER  ";
  cin>>mrno;
   while(fout.read((char*)&obj_6,recsize))
  {
     if(obj_6.giveaccountno()==mrno)
     {
        clrscr();
        cout<<"\n\tMODIFY MENU";
        obj_6.dispdata();
        int tmprno=obj_6.giveaccountno()-1;
         account obj_7;
        cout<<"\n\t ENTER NEW DATA";
         obj_7.getdata(tmprno);
        char confirm;
        cout<<" \n\tARE YOU SURE(Y/N)??";
        cin>>confirm;
     if(confirm=='Y'||confirm=='y')
        {
         fout.seekg(-1*recsize,ios::cur);
         fout.write((char*)&obj_7,recsize);
         cout<<"\n\tRECORD MODIFIED  ";
         cout<<"\n\tPress any key.....";
         flag=1;
        }
     }
      if(flag==0)
     {
         cout<<"\n\tNO SUCH RECORD EXIST";
         cout<<"\n\tPress any key.....";
  }
     }
         fout.close();
         getch();
   }


   void withdraw()
   {
         account obj_9;
     fstream fout;
   int mrno=0;
 int recsize=sizeof(account);
        clrscr();
         cout<<"\n\tWITHDRAWAL MENU";
       fout.open("banking.txt",ios::in|ios::out|ios::binary);
         if(!fout)
    {
        cout<<"\nFILE OPEN ERROR  ";getch();return;}
         fout.seekg(ios::beg);
        cout<<" \n\tENTER ACCOUNT NUMBER  ";
     cin>>mrno;
         while(fout.read((char*)&obj_9,recsize))
{
         if(obj_9.giveaccountno()==mrno)
     {
      clrscr();
      cout<<"\n\tENTER THE AMOUNT TO BE WITHDRAWED::Rs ";
   cin>>w;
         obj_9.withdrawal(w);
         fout.seekg(-1*recsize,ios::cur);
        fout.write((char*)&obj_9,recsize);
 }
   }
       fout.close();
         getch();
 }

//------------------starting page-------------

void process(void)
{
int d=DETECT,g,size;
void *image;
initgraph(&d,&g,"c:/tc/bgi");
size=imagesize(0,0,638,100);
image=malloc(size);
clrscr();
setcolor(RED);
settextstyle(1,0,9);
outtextxy(80,10,"BANKING SYSTEM");
getimage(5,5,500,100,image);
cleardevice();
for(int count=1;count<300;count+=2)
putimage(0,10+count,image,COPY_PUT);

settextstyle(2,0,8);
outtextxy(170,370,"loading...");
setcolor(BLUE);
rectangle(170,400,500,420);
getch();
setcolor(BLACK);
setbkcolor(BLACK);
}

void deposit(void)
{
account obj_10;
     fstream fout;
      int mrno=0;
     int recsize=sizeof(account);
    clrscr();
         cout<<"\n\tDEPOSITAL MENU";
     fout.open("banking.txt",ios::in|ios::out|ios::binary);
  if(!fout)
{
       cout<<"FILE OPEN ERROR  ";
       getch();
       return;}
    fout.seekg(ios::beg);
       cout<<"\n\tENTER ACCOUNT NUMBER  ";
     cin>>mrno;
       while(fout.read((char*)&obj_10,recsize))
 {
     if(obj_10.giveaccountno()==mrno)
 {
   clrscr();
    cout<<"\n\tENTER THE AMOUNT TO BE DEPOSITED ::Rs ";
  cin>>w;
     obj_10.deposital(w);
 fout.seekg(-1*recsize,ios::cur);
     fout.write((char*)&obj_10,recsize);
  }
    }
 fout.close();
    getch();
  }


Answer
Hello Khushi.

I commend you for doing so much of your assignment. I've tested all of the functions and made slight changes. My comments in the code start with //=== or /*===.

Some of the problems were caused by the deposit and withdraw functions. In those functions you need to quit the loop as soon as the record is modified. Somehow the write of the record prevents the end of file from being detected on the read.

Please let me know if you have any more questions.

You should continue testing the code.

The changed code is below. I've included only changed functions.

In class account:
=====================================================

   void getdata(int mrno)
   {
       accountno=mrno+1;
       cout<<"\n\tACCOUNT NUMBER        ::  ";
       cout<<accountno<<" ";
       cout<<"\n\tENTER YOUR NAME       ::  ";
       /*===gets(name) gets did not work on my system.
       you really need a way to prevent buffer overruns.
       its better to cin into a temporary string, then copy the string to name
       */
       cin >> name;

       cout<<" ";
       cout<<"\n\tENTER TYPE OF ACCOUNT SAVING(s)/CURRENT(c)  ::  ";
       cin>>type;
       cout<<" ";
       cout<<"\n\tENTER INITIAL AMOUNT  ::Rs  ";cin>>deposit;
       cout<<" ";
   }

In the rest of the program:
=====================================================

void addrecord()
{
   account obj_1,obj_2;
   fstream fout;
   fout.open("banking.txt",ios::in|ios::binary);
   /*if(!fout)
   {
   cout<<"FILE OPEN ERROR  ";
   getch();
   return;
   } */
   int recsize=sizeof(account);
   fout.seekg(0,ios::end);

   int mrno = 0;
   /*=== if the file is empty, the next seekg will fail and mrno will not get initialized
   so it's initialized to 0 above.
   */
   fout.seekg(-1*recsize,ios::cur/*=== |ios::binary*/); //=== iso::binary is not valid for seek


   //=== Initialize mrno from the file only if seek passed
   if (!fout.fail())
   {
       fout.read((char*)&obj_1,recsize);
       mrno=obj_1.giveaccountno();
   }
   else
   {
       fout.clear();
   }

   fout.close();
   clrscr();
   cout<<"\nADD MENU";
   obj_2.getdata(mrno);
   fout.clear(); //=== added. Clear all previous errors so that we can check for open success
   fout.open("banking.txt",ios_base::app|ios_base::binary);
   if(!fout)
   {
       cout<<"FILE OPEN ERROR  ";
       getch();
       return;
   }
   fout.write((char*)&obj_2,recsize);
   cout<<"\n\tRECORD ADDED TO DATABASE"<<"\nPress any key to continue... ";
   getch();
   fout.close();
}

void delrecord()
{
   account obj_5;
   fstream fout,temp;
   int mrno;
   int flag = 0; //=== you must initialize flag because it is initialized below only if the record is found.
   int recsize=sizeof(account);
   clrscr();
   cout<<"\n\t CLOSE ACCOUNT MENU ";
   fout.open("banking.txt",ios::in|ios::binary);
   if(!fout)
   {
       cout<<"\n\tFILE OPEN ERROR  ";
       getch();
       return;
   }
   temp.open("temp.txt",ios::app|ios::binary);
   if(!temp)
   {
       cout<<"\n\tFILE OPEN ERROR  ";
       getch();
       return;
   }
   cout<<" \n\tENTER THE ACCOUNT NUMBER ";
   cin>>mrno;
   while(fout.read((char*)&obj_5,recsize))
   {
       if(obj_5.giveaccountno()==mrno)
       {
         obj_5.dispdata();
         char confirm;
         cout<<"\n\tARE YOU SURE TO DELETE IT(Y/N)..";cin>>confirm;
         if(confirm=='Y'||confirm=='y')
         {
         fout.read((char*)&obj_5,recsize);
         cout<<"\n\tRECORD DELETED FORM DATABASE";
         cout<<"\n\tpress any key....";
         flag=1;
         if(!fout)
         break;
         }
         flag=1;
       }
       temp.write((char*)&obj_5,recsize);
   }
   fout.close();
   temp.close();
   remove("banking.txt");
   rename("temp.txt","banking.txt");
   if(flag==0)
   {
       cout<<"\n\tNO SUCH ACCOUNT EXIST";
       cout<<"\n\tPress any key.....";
   }
   getch();
}

void modrecord()
{
   account obj_6;
   fstream fout;
   int mrno,flag=0;
   int recsize=sizeof(account);
   clrscr();
   cout<<"\n\tMODIFY RECORD MENU ";
   fout.open("banking.txt",ios::in|ios::out|ios::binary);
   if(!fout)
   {
       cout<<"FILE OPEN ERROR  ";
       getch();
       return;
   }
   fout.seekg(ios::beg);
   cout<<"\n\tENTER RECORD NUMBER  ";
   cin>>mrno;
   while(fout.read((char*)&obj_6,recsize))
   {
       if(obj_6.giveaccountno()==mrno)
       {
         clrscr();
         cout<<"\n\tMODIFY MENU";
         obj_6.dispdata();
         int tmprno=obj_6.giveaccountno()-1;
         account obj_7;
         cout<<"\n\t ENTER NEW DATA";
         obj_7.getdata(tmprno);
         char confirm;
         cout<<" \n\tARE YOU SURE(Y/N)??";
         cin>>confirm;
         flag=1; /*=== move this up from below.
         I think you mean flag to show that
         a record was found, not that a record
         was modified */

         if(confirm=='Y'||confirm=='y')
         {
         fout.seekg(-1*recsize,ios::cur);
         fout.write((char*)&obj_7,recsize);
         cout<<"\n\tRECORD MODIFIED  ";
         cout<<"\n\tPress any key.....";
         }

         break; //=== once the record is found, quit.
       }
   }
   
   //=== move this out of the loop
   if(flag==0)
   {
       cout<<"\n\tNO SUCH RECORD EXIST";
       cout<<"\n\tPress any key.....";
   }

   fout.close();
   getch();
}


void withdraw()
{
   account obj_9;
   fstream fout;
   int mrno=0;
   int recsize=sizeof(account);
   clrscr();
   cout<<"\n\tWITHDRAWAL MENU";
   fout.open("banking.txt",ios::in|ios::out|ios::binary);
   if(!fout)
   {
       cout<<"\nFILE OPEN ERROR  ";getch();return;}
   fout.seekg(ios::beg);
   cout<<" \n\tENTER ACCOUNT NUMBER  ";
   cin>>mrno;
   while(fout.read((char*)&obj_9,recsize))
   {
       if(obj_9.giveaccountno()==mrno)
       {
         clrscr();
         cout<<"\n\tENTER THE AMOUNT TO BE WITHDRAWED::Rs ";
         cin>>w;
         obj_9.withdrawal(w);
         fout.seekg(-1*recsize,ios::cur);
         fout.write((char*)&obj_9,recsize);

         break; //=== Once the record is found, break out of the loop;
       }
   }
   fout.close();
   getch();
}

void deposit(void)
{
   account obj_10;
   fstream fout;
   int mrno=0;
   int recsize=sizeof(account);
   clrscr();
   cout<<"\n\tDEPOSITAL MENU";
   fout.open("banking.txt",ios::in|ios::out|ios::binary);
   if(!fout)
   {
       cout<<"FILE OPEN ERROR  ";
       getch();
       return;}
   fout.seekg(ios::beg);
   cout<<"\n\tENTER ACCOUNT NUMBER  ";
   cin>>mrno;
   while(fout.read((char*)&obj_10,recsize))
   {
       if(obj_10.giveaccountno()==mrno)
       {
         clrscr();
         cout<<"\n\tENTER THE AMOUNT TO BE DEPOSITED ::Rs ";
         cin>>w;
         obj_10.deposital(w);
         fout.seekg(-1*recsize,ios::cur);
         fout.write((char*)&obj_10,recsize);

         break; //=== Once the record is found, break out of the loop;
       }
   }
   fout.close();
   getch();
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.