text file

hi can anyone help me in this program.if i delete one record usig the id the program will delete all the record in the text file.please help me tq

#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
#include <string.h>
int AddRecord(int i);
int DisplayRecord(int i);
int EditRecord (int i);
int DeleteRecord (int i);
int SearchRecord (int i);
struct{
int id;
char passportno[10];
char firstname[50];
char lastname[50];
char originatingcity[50];
char destinationcity[50];
char flightnumber[50];
char departuretime[50];
char departuredate[50];
char totalcost[10];
char seatclass[10];
char seatno[4];

}mv[100];
int main()
{
int iname=0;
fstream f;
int i=0;
ifstream ifile;
ifile.open("SaveData.txt",ios::nocreate);
while(ifile.peek()!=EOF)
{
ifile>>mv[i].id;
ifile>>mv[i].passportno;
ifile>>mv[i].firstname;
ifile>>mv[i].lastname;
ifile>>mv[i].originatingcity;
ifile>>mv[i].destinationcity ;
ifile>>mv[i].flightnumber;
ifile>>mv[i].departuretime;
ifile>>mv[i].departuredate;
ifile>>mv[i].totalcost;
ifile>>mv[i].seatclass;
ifile>>mv[i].seatno;
i++; //recordincrement
}
ifile.close();
while(true)
{
int ch;
system("cls");
cout<<endl;
cout<<"\t** WELCOME TO THIRU AIRLINES CUSTOMER REGISTRATION SYSTEM DATABASE **"<<endl;
cout<<endl;
cout<<"\t\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n"<<endl;
cout<<"\t\t\t--PLEASE ENTER COUSTOMER PERSONAL DETAILS--"<<endl;
cout<<endl;
cout<<"\t\t\t1.Add Customer Record"<<endl;
cout<<"\t\t\t2.Display All The Customers Records"<<endl;
cout<<"\t\t\t3.Search Customers Record"<<endl;
cout<<"\t\t\t4.Edit Customer Record"<<endl;
cout<<"\t\t\t5.Delete Customer Record"<<endl;
cout<<"\t\t\t6.Exit\n"<<endl;
cout<<endl;
cout<<"\t\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
cout<<"\t\t\t Enter your Choice (1-6)\n\n";
cin>>ch;
switch (ch)
{
case 1:
i=AddRecord(i);
break;
case 2:
DisplayRecord(i);
break;
case 3:
SearchRecord(i);
break;
case 4:
EditRecord(i);
break;
case 5:
i=DeleteRecord(i);
break;
case 6:
cout<<"\n\n Please Check All The Details of Customers Before You Exit This Program"<<endl;
cout<<"\t\t\t\tTHANK YOU"<<endl;
exit(1);
break;
default:
cout<<"\n\nlnvalid choice!!!"<<endl;
system("pause");
break;
}
}
return 0;
}
int AddRecord(int i)
{
int iname=0;
char reply;
int x;
ofstream ofile;
while(1)
{
system("cls");
cout<<"\t\t**************Add Customer Record***************\n\n"<<endl;
mv[i].id=i+1;
cout<<"Enter Passport No:"<<endl;
cin>>mv[i].passportno;
cout<<"Enter First Name:"<<endl;
cin>>mv[i].firstname;
cout<<"Enter Last Name:"<<endl;
cin>>mv[i].lastname;
cout<<"Enter Originating City:"<<endl;
cin>>mv[i].originatingcity;
cout<<"Enter Destination City"<<endl;
cin>>mv[i].destinationcity ;
cout<<"Enter Flight Number:"<<endl;
cin>>mv[i].flightnumber;
cout<<"Enter Departure Time:"<<endl;
cin>>mv[i].departuretime;
cout<<"Enter Departure Date:"<<endl;
cin>>mv[i].departuredate;
cout<<"Total Cost:"<<endl;
cin>>mv[i].totalcost;
cout<<endl;
cout<<"\t\t~~~~~Choose preferred class as stated below~~~~~\n\n"<<endl;
cout<<endl;
cout<<"\t\tBusiness Class ---> BC\n\n"<<endl;
cout<<"\t\tFirst Class ----> FC\n\n"<<endl;
cout<<"\t\tEconomy Class --->EC\n\n"<<endl;
cout<<"Enter Seat Class:"<<endl;
cin>>mv[i].seatclass;
cout<<"Enter Seat No:"<<endl;
cin>>mv[i].seatno;
i++;
cout<<"DATA ADDED SUCCESSFULLY!\n";
cout<<endl;
//saving record
ofile.open("SaveData.txt",ios::trunc);
for(x=0;x<i;x++)
{
ofile<<mv[x].id<<endl;
ofile<<mv[x].passportno<<endl;
ofile<<mv[x].firstname<<endl;
ofile<<mv[x].lastname<<endl;
ofile<<mv[x].originatingcity<<endl;
ofile<<mv[x].destinationcity<<endl;
ofile<<mv[x].flightnumber<<endl;
ofile<<mv[x].departuretime<<endl;
ofile<<mv[x].departuredate<<endl;
ofile<<mv[x].totalcost<<endl;
ofile<<mv[x].seatclass<<endl;
ofile<<mv[x].seatno<<endl;
}
cout<<"\nContinue? (y/n):";
cin>>reply;
if(reply=='n')
{
return i;
break;
}
}
return i;
}

int DisplayRecord(int i)
{
int iname=0;
int y;
system("cls");
cout<<"\t\t**************Display Customers Record***************\n\n"<<endl;
for(y=0;y<i;y++)
{
cout<<"\t\t[Id] \t\t="<<mv[y].id<<endl;
cout<<"\t\t[Passport No \t\t="<<mv[y].passportno<<endl;
cout<<"\t\t[First Name \t\t="<<mv[y].firstname<<endl;
cout<<"\t\t[Last Name] \t\t="<<mv[y].lastname<<endl;
cout<<"\t\t[Originating City]\t\t="<<mv[y].originatingcity<<endl;
cout<<"\t\t[Destination City]\t\t="<<mv[y].destinationcity<<endl;
cout<<"\t\t[Flight Number] \t\t="<<mv[y].flightnumber<<endl;
cout<<"\t\t[Departure Time] \t\t="<<mv[y].departuretime<<endl;
cout<<"\t\t[Departure Date] \t\t="<<mv[y].departuredate<<endl;
cout<<"\t\t[Total Cost] \t\t="<<mv[y].totalcost<<endl;
cout<<"\t\t[Seat Class] \t\t="<<mv[y].seatclass<<endl;
cout<<"\t\t[Seat No] \t\t="<<mv[y].seatno<<endl;
}
cout<<"\t\t------End Of Report------\n\n"<<endl;
system("pause");
return 0;
}
int EditRecord(int i)
{
int x;
int iname=0;
ofstream ofile;
if (i<=0)
{
cout<<"\nNothing to Edit!\n";
system("pause");
return i;
}
int ID,z;
system("cls");
cout<<"\t\t**************Edit Customer Record***************\n\n"<<endl;
cout<<"\nEnter the Id number you want to edit:";
cin>>ID;
if(ID > i||ID<0)
{
cout<<"\nNot valid ID!\n";
system("pause");
return i;
}
for(z=0;z<=i;z++)
{
if(ID==mv[z].id)
{
cout<<"\nEditing Record for ID:"<<mv[z].id<<endl<<endl<<endl;
cout<<"OLD Passport No:"<<mv[z].passportno;
cout<<"-->NEW Passport No:";
cin>>mv[z].passportno;
cout<<"OLD first Name:"<<mv[z].firstname;
cout<<"-->NEW First Name:";
cin>>mv[z].firstname;
cout<<"OLD last Name:"<<mv[z].lastname;
cout<<"-->NEW Last Name:";
cin>>mv[z].lastname;
cout<<"OLD Originating City:"<<mv[z].originatingcity;
cout<<"-->NEW Originating City:";
cin>>mv[z].originatingcity;
cout<<"OLD Destination City:"<<mv[z].destinationcity;
cout<<"-->NEW Destination City:";
cin>>mv[z].destinationcity;
cout<<"OLD Flight Number:"<<mv[z].flightnumber;
cout<<"-->NEW Flight Number:";
cin>>mv[z].flightnumber;
cout<<"OLD Departure Time:"<<mv[z].departuretime;
cout<<"-->NEW Departure Time:";
cin>>mv[z].departuretime;
cout<<"OLD Departure Date:"<<mv[z].departuredate;
cout<<"-->NEW Departure Date:";
cin>>mv[z].departuredate;
cout<<"OLD Total Cost:"<<mv[z].totalcost;
cout<<"-->NEW Total Cost:";
cin>>mv[z].totalcost;
cout<<"Seat Class:"<<mv[z].seatclass<<endl;;
cout<<"\t\t~~~~~Choose preferred class as stated below~~~~~\n\n"<<endl;
cout<<endl;
cout<<"\t\tBusiness Class ---> BC\n\n"<<endl;
cout<<"\t\tFirst Class ----> FC\n\n"<<endl;
cout<<"\t\tEconomy Class --->EC\n\n"<<endl;
cout<<"-->NEW Seat Class:";
cin>>mv[z].seatclass;
cout<<"OLD Seat No:"<<mv[z].seatno;
cout<<"-->NEW Seat No:";
cin>>mv[z].seatno;
cout<<endl;

cout<<"DATA EDITED SUCCESSFULLY!";
system("pause");
}
}
ofile.open("SaveData.txt",ios::trunc);
if(ofile.fail())
{
cout<<"error writting to directory\n\n";
}
for(x=0;x<i;x++)
{
ofile<<mv[x].id<<endl;
ofile<<mv[x].passportno<<endl;
ofile<<mv[x].firstname<<endl;
ofile<<mv[x].lastname<<endl;
ofile<<mv[x].originatingcity<<endl;
ofile<<mv[x].destinationcity<<endl;
ofile<<mv[x].flightnumber<<endl;
ofile<<mv[x].departuretime<<endl;
ofile<<mv[x].departuredate<<endl;
ofile<<mv[x].totalcost<<endl;
ofile<<mv[x].seatclass<<endl;
ofile<<mv[x].seatno<<endl;
}
ofile.close();
return 0;
}

the program continue in next line
Hi,
You are passing variable "i" by value in every functions which may give incorrect result. Either pass by reference or use pointers and modify all the functions accordingly .
Also I see mismatch of opening and closing braces.
- You're using the std namespace without declaring so.

- You're using functions from cstdlib.h without including it.

- Your spacing is quite honestly attrocious, this will not effect your code but it gives a negative impression about your skill level, as well as making your code almost impossible to read.

- There is no output to the screen before your first clear screen command. This shows a disregard for what the user is doing before they call your program.

- This will help with your display: http://www.cplusplus.com/reference/iostream/ios/fill/
As of right now any changes you make to the wording would require you to sit there and redo the display by hand, this is a waste.

- Your "while(...)" loops look to be never ending, but to be honest I'm having a hard time reading them due to the lack of spacing.
Last edited on
Topic archived. No new replies allowed.