plz rectify the errors in this code for a phonebook

the code executes just fine but no data is stored in the txt file i cr

Page 1 of 1

2 Replies - 722 Views - Last Post: 14 November 2007 - 03:28 PM Rate Topic: -----

#1 urvashi_38  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-November 07

plz rectify the errors in this code for a phonebook

Posted 14 November 2007 - 01:36 PM

ok this is very elementary i know but can somebody tell me what eactly is the problem with this code?? i hav tried evrything possible....i immediately need sum help!!
i am attachin the .cpp file with the post

//************PROGRAM FOR A PERSONAL TELEPHONE DIARY****************** //
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>				  //Header files included
int  ch,recno,i,number;	//global variables declared
char username[30],address[90],email[50];
char phone[15];				 //arrays declared
ifstream fin;  // file stream for input
ofstream fout; // file stream for output
//*********FUNCTIONS DECLARED***********
void enterrec();	 //function for entering records
void disrec();	   //function for displaying records
void search();	   //function for seaching for a record
void modrec();	   //function for modifying records
void delrec();	   //function for deleting records
void quit();		 //function for quitting
void main()		  //beginning of executable part of the program
{
  // get number of records in the file
  fin.open("teldiar.txt");
  while(fin)
  {
  //if(fin.eof()) break;
  //reading from file
  fin.getline(username,30);
  fin.getline(phone,15);
  fin.getline(email,50);
  fin.getline(address,90);
  recno++;
  }
  fin.close();
  recno--;

  do
  {
  clrscr();
  //DISPLAYING MAIN MENU OF THE PROGRAM
  cout<<"\t\t\tWELCOME TO YOUR OWN TELEPHONE DIARY"
	  <<"\n\t\t\tWHICH STORES INFORMATION ABOUT YOUR"
	  <<" \n\t\t\tACQUAINTANCES AND WILL HELP YOU RECALL ANY,"
	  <<"\n\t\t\tNAME,NUMBER,ADDRESS OR E-MAIL ID---"
	  <<"\n\t\t\tIN A MATTER OF A FEW MOMENTS!!";
  cout<<"\n\n\t\t\t=========";
  cout<<"\n\t\t\tMAIN MENU"<<endl;
  cout<<"\t\t\t========="<<endl<<endl;
  cout<<"\t\t [1] ENTER RECORDS" <<endl
	  <<"\t\t [2] DISPLAY RECORDS " <<endl
	  <<"\t\t [3] SEARCH FOR A RECORD "<<endl
	  <<"\t\t [4] MODIFY A RECORD "<<endl
	  <<"\t\t [5] DELETE A RECORD"<<endl
	  <<"\t\t [6] QUIT"<<endl;
  cout<<"\n\t"<<'\t'<<"ENTER YOUR CHOICE: ";
  ch=0;
  cin>>ch;	  //enters choice of the user
  cin.get();
  switch(ch)
  {
   case 1:
	 enterrec() ;//function for entering
		   //records called
	 break;
   case 2:
	 disrec();   //function for displaying
		   //records called
	 break;
   case 3:
	 search();  //function for searching
		 //a record called
	 break;
   case 4:
	 modrec();//function for modifying
	   //a record called
	 break;
   case 5:
	 delrec();//function for deleting
	   //a record called
	 break;
   case 6:
	 quit();  //function for quitting
	   //called
	 break;
   default:
	 clrscr();
	 cout<<"\n\n\n\n\n\n\n\t\t\tSORRY...."
		 <<"INVALID CHOICE!!";
	 //displaying error message
	 getch();
  }
   } while(ch!=6);
}
//-------------------------------------------------------------//
//****************FUNCTION FOR ENTERING RECORDS****************//
//-------------------------------------------------------------//
void enterrec()
{
 clrscr();
 /*cout<<"\t\t\tMaximum records that can be entered:\t"<<MAX;
 cout<<"\n\t\t\tRecords already entered:"<<'\t'<<(last+1)<<endl;*/
 cout<<"\t\t\tEnter number of records to be entered: ";
 cin>>recno;
 cin.get();
 ifstream fin("teldiar.txt");
 ofstream fout("temp.txt");
 //write already existing records of teldiar to temp
 while(fin)
 {
	if(fin.eof())
	break;
  fin.getline(username,30);
  fin.getline(phone,15);
  fin.getline(email,50);
  fin.getline(address,90);
  //reading from file
  fout<<username<<'\n'
	  <<phone<<'\n'
	  <<email<<'\n'
	  <<address<<'\n';//writing data to file
 }
 for(i=1;i<=recno;i++)
 {
  //ENTERING INFORMATION FOR (permanent) STORAGE
  cout<<"\n\n\t\t\tENTER NAME: ";
  cin.getline(username,30);
  cout<<"\t\t\tENTER "<<username<<"'s PHONE NUMBER: ";
  cin.getline(phone,15);
  cout<<"\t\t\tENTER "<<username<<"'s EMAIL ID: ";
  cin.getline(email,50);
  cout<<"\t\t\tENTER "<<username<<"'s ADDRESS: ";
  cin.getline(address,90);
  cout<<"\t\t\t----------------------------------------";
  fout<<username<<'\n'
	  <<phone<<'\n'
	  <<email<<'\n'
	  <<address<<'\n';//writing data to file
 }
 //fout.close();
 //fin.close();
 remove("teldiar.txt");
 rename("temp.txt","teldiar.txt");
 fin.close();
 fout.close();
 }
  //********************END OF FUNCTION************************//
  //-----------------------------------------------------------//
  //**************FUNCTION FOR DISPLAYING RECORDS**************//
void disrec()
{
  clrscr();
  ifstream fin("teldiar.txt");
	//opening file for reading
  cout<<" \n\n\t\tYOUR PERSONAL DIARY--- " <<endl<<endl;
  cout<<"REC#"
   <<"\tNAME"
   <<"\tPHONE#"
   <<"\tEMAIL ID"
   <<"\t\tADDRESS";
  for(int number = 1; number<= recno; number++)
  {
  fin.getline(username,30);
  fin.getline(phone,15);
  fin.getline(email,50);
  fin.getline(address,90);
  //reading from file

  cout<<endl;
  cout<<number;
  cout<<'\t'<<username;
  cout<<'\t'<<phone;
  cout<<'\t'<<email;
  cout<<"\t\t"<<address;
  //displaying all the records of the file on to the screen
  } //end of for

 fin.close(); //closing the stream
 cout<<endl;
 getch();
}
  //*******************END OF FUNCTION*************************//
  //-----------------------------------------------------------//
  //*******************FUNCTION FOR SEARCHING******************//
void search()
{
 ifstream fin("teldiar.txt");
	//opening file for reading
 clrscr();
 char sname[30]; //temporary array for
   //name to be searched declared
 int d; //local variable `d' declared
 cout<<"\t\t\n\nEnter the name to be searched: ";
 cin.getline(sname,30);
 int flag = 0; //local variable `flag' initialized to 0
 for(int i = 1; i<=recno; i++)
 {
  fin.getline(username,30);
  fin.getline(phone,15);
  fin.getline(email,50);
  fin.getline(address,90);
  //reading from file

  d=strcmp(sname,username);//comparing ascii values
		 //of all name present with
		 //the name entered
  if(d==0) //i.e if the diff is=0(the names are same)
  {
	 flag=1;
	 //Displaying corresponding information for the name
	 cout<<"\n\t\tNAME		: "<<username<<endl;
	 cout<<"\t\tADDRESS	 : "<<address<<endl;
	 cout<<"\t\tPHONE NUMBER: "<<phone<<endl;
	 cout<<"\t\tEMAIL ID	: "<<email<<endl;
	 cout<<"\n\n\t\t\tTHANKYOU!!";
	 cout << endl;
  } //end of if
 }//end of for
  if(flag==0)
  {
	 cout<<"\n\t\t\tNAME NOT FOUND...PLEASE VERIFY!!";
	 //displaying error message
  }
 fin.close();   //closing the stream
 getch();
} //end of search
  //*****************END OF FUNCTION***************************//
  //-----------------------------------------------------------//
  //*************FUNCTION FOR MODIFYING RECORDS****************//
void modrec()
{
  ofstream fout("temp.txt");
	//opening file for writing
  ifstream fin("teldiar.txt");
	//opening file for reading
  char sname[30];  //local array for
	 //name to be modified declared
  int d,flag=0;	//local variables declared
  char ch1;		//local char variable for choice declared
  clrscr();
  cout<<"\t\tEnter name for which the record is to be modified: ";
  cin.getline(sname,30);
  for(int i = 1; i <= recno; i++)
  {
  fin.getline(username,30);
  fin.getline(phone,15);
  fin.getline(email,50);
  fin.getline(address,90);
  //reading from file
  d=strcmp(sname,username);//comparing the name entered
		 //with all the names present
  if(d==0) //i.e if the names are same
  {
   flag=1;
   // Display original data
   cout<<"\t\tNAME:"<<username;
   cout<<"\n\t\tPHONE NUMBER:"<<phone;
   cout<<"\n\t\tADDRESS:"<<address;
   cout<<"\n\t\tEMAIL ID:"<<email;
   cout<<"\n\nDo you want to modify the record (Y/N): ";
   cin>>ch1;
   cin.get();
   // Entering new(modified) data from the user
   if(ch1=='y' || ch1 == 'Y')
   {
	cout<<"\n\t\tEnter name: ";
	cin.getline(username,30);
	cout<<"\t\tEnter new telephone number: ";
	cin.getline(phone,15);
	cout<<"\t\tEnter new address: ";
	cin.getline(address,90);
	cout<<"\t\tEnter changed e-mail id: ";
	cin.getline(email,50);
   } //end of if
  }//end of if(d==0)
		fout<<username<<'\n'
	  <<phone<<'\n'
	  <<email<<'\n'
	  <<address<<'\n';//writing data to file
 }  //end of for
 fin.close();
 fout.close();  //closing both the streams
 remove("teldiar.txt");
 rename("temp.txt","teldiar.txt");
 //removing old file(teldiar),and renaming the new file(temp)
 //to teldiar
 if(flag==0)
 {
   cout<<"\nNAME NOT FOUND ...PLEASE VERIFY";
 }	//end of if(flag==0)
}//end of modification
  //******************END OF FUNCTION**************************//
  //-----------------------------------------------------------//
  //******************FUNCTION FOR DELETING********************//
  void delrec()
  {
 ofstream fout("temp.txt");
	//opening file for writing
 ifstream fin("teldiar.txt");
	//opening file for reading
 clrscr();
 char ch;  //local variable for choice
 int temprec = recno;	//local variable declared
 for(int i = 1; i <= temprec; i++)
 {
  fin.getline(username,30);
  fin.getline(phone,15);
  fin.getline(email,50);
  fin.getline(address,90);
  //reading from file

  // Display original data
   cout<<"\t\tNAME:"<<username;
   cout<<"\n\t\tPHONE NUMBER:"<<phone;
   cout<<"\n\t\tADDRESS:"<<address;
   cout<<"\n\t\tEMAIL ID:"<<email;
  cout<<"\n\tDo you wish to delete this record?(Y/N)";
  cin>>ch;
  if(ch=='Y'|| ch=='y')
  {
   continue; // do not write to file
  }
  else
  {
	  fout<<username<<'\n'
   <<phone<<'\n'
   <<email<<'\n'
   <<address<<'\n';//writing data to file
  }
  }//end of while
 fout.close();
 fin.close();	 //closing both the streams
 remove("teldiar.txt");
 rename("temp.txt","teldiar.txt");
 //removing old file(teldiar),and renaming the new file(temp)
 //to teldiar
  }//end  of deleting
  //******************END OF FUNCTION**************************//
  //-----------------------------------------------------------//
  //**************FUNCTION FOR QUITTING************************//
  void quit()
  {
  clrscr();
  cout<<"\n\t\t\t\THANKYOU FOR VISITING!!";
  cout<<"\n\n\n\t\t\tPress any key...";
  //displaying message on the screen
  getch();
  }
  //******************END OF FUNCTION**************************//
  //-----------------------------------------------------------//
  //******************END OF PROGRAM***************************//
________________________________________
Be a better sports nut! Let your teams follow you with Yahoo Mobile. Try it now.
Plain Text Attachment [ Scan and Save to Computer ] 
//************PROGRAM FOR A PERSONAL TELEPHONE DIARY******************
 //
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>				  //Header files included
int  ch,recno,i,number;	//global variables declared
char username[30],address[90],email[50];
char phone[15];				 //arrays declared

ifstream fin; 	// file stream for input
ofstream fout;	// file stream for output

//*********FUNCTIONS DECLARED***********
void enterrec();	 //function for entering records
void disrec();	   //function for displaying records
void search();	   //function for seaching for a record
void modrec();	   //function for modifying records
void delrec();	   //function for deleting records
void quit();		 //function for quitting

void main()		  //beginning of executable part of the program
{
  // get number of records in the file
  fin.open("teldiar.txt");
  while(fin)
  {
		//if(fin.eof()) break;

		//reading from file
		fin.getline(username,30);
		fin.getline(phone,15);
		fin.getline(email,50);
		fin.getline(address,90);
		recno++;
  }
  fin.close();
  recno--;


  do
  {
		clrscr();
		//DISPLAYING MAIN MENU OF THE PROGRAM
		cout<<"\t\t\tWELCOME TO YOUR OWN TELEPHONE DIARY"
			<<"\n\t\t\tWHICH STORES INFORMATION ABOUT YOUR"
			<<" \n\t\t\tACQUAINTANCES AND WILL HELP YOU RECALL ANY,"
			<<"\n\t\t\tNAME,NUMBER,ADDRESS OR E-MAIL ID---"
			<<"\n\t\t\tIN A MATTER OF A FEW MOMENTS!!";
		cout<<"\n\n\t\t\t=========";
		cout<<"\n\t\t\tMAIN MENU"<<endl;
		cout<<"\t\t\t========="<<endl<<endl;
		cout<<"\t\t [1] ENTER RECORDS" <<endl
			<<"\t\t [2] DISPLAY RECORDS " <<endl
			<<"\t\t [3] SEARCH FOR A RECORD "<<endl
			<<"\t\t [4] MODIFY A RECORD "<<endl
			<<"\t\t [5] DELETE A RECORD"<<endl
			<<"\t\t [6] QUIT"<<endl;

		cout<<"\n\t"<<'\t'<<"ENTER YOUR CHOICE: ";
		ch=0;
		cin>>ch;	  //enters choice of the user
		cin.get();

		switch(ch)
		{
			case 1:
					enterrec() ;//function for entering
							 //records called
					break;
			case 2:
					disrec();   //function for displaying
							 //records called
					break;
			case 3:
					search();  //function for searching
						   //a record called
					break;
			case 4:
					modrec();//function for modifying
						 //a record called
					break;
			case 5:
					delrec();//function for deleting
						 //a record called
					break;

			case 6:
					quit();  //function for quitting
						 //called
					break;
			default:
					clrscr();
					cout<<"\n\n\n\n\n\n\n\t\t\tSORRY...."
						<<"INVALID CHOICE!!";
					//displaying error message
					getch();
		}
   } while(ch!=6);
}
//-------------------------------------------------------------//
//****************FUNCTION FOR ENTERING RECORDS****************//
//-------------------------------------------------------------//
void enterrec()
{
	clrscr();
	/*cout<<"\t\t\tMaximum records that can be entered:\t"<<MAX;
	cout<<"\n\t\t\tRecords already entered:"<<'\t'<<(last+1)<<endl;*/
	cout<<"\t\t\tEnter number of records to be entered: ";
	cin>>recno;
	cin.get();
	ifstream fin("teldiar.txt");
	ofstream fout("temp.txt");
	//write already existing records of teldiar to temp
	while(fin)
	{
	   if(fin.eof())
	   break;
		fin.getline(username,30);
		fin.getline(phone,15);
		fin.getline(email,50);
		fin.getline(address,90);
		//reading from file

		fout<<username<<'\n'
			<<phone<<'\n'
			<<email<<'\n'
			<<address<<'\n';//writing data to file
	}
	for(i=1;i<=recno;i++)
	{

		//ENTERING INFORMATION FOR (permanent) STORAGE
		cout<<"\n\n\t\t\tENTER NAME: ";
		cin.getline(username,30);
		cout<<"\t\t\tENTER "<<username<<"'s PHONE NUMBER: ";
		cin.getline(phone,15);
		cout<<"\t\t\tENTER "<<username<<"'s EMAIL ID: ";
		cin.getline(email,50);
		cout<<"\t\t\tENTER "<<username<<"'s ADDRESS: ";
		cin.getline(address,90);
		cout<<"\t\t\t----------------------------------------";

		fout<<username<<'\n'
			<<phone<<'\n'
			<<email<<'\n'
			<<address<<'\n';//writing data to file

	}

	//fout.close();
	//fin.close();
	remove("teldiar.txt");
	rename("temp.txt","teldiar.txt");
	fin.close();
	fout.close();
	}
  //********************END OF FUNCTION************************//
  //-----------------------------------------------------------//
  //**************FUNCTION FOR DISPLAYING RECORDS**************//

void disrec()
{

	 clrscr();
	 ifstream fin("teldiar.txt");
				//opening file for reading

	 cout<<" \n\n\t\tYOUR PERSONAL DIARY--- " <<endl<<endl;
	 cout<<"REC#"
		 <<"\tNAME"
		 <<"\tPHONE#"
		 <<"\tEMAIL ID"
		 <<"\t\tADDRESS";
	 for(int number = 1; number<= recno; number++)
	 {

		fin.getline(username,30);
		fin.getline(phone,15);
		fin.getline(email,50);
		fin.getline(address,90);
		//reading from file


		cout<<endl;
		cout<<number;
		cout<<'\t'<<username;
		cout<<'\t'<<phone;
		cout<<'\t'<<email;
		cout<<"\t\t"<<address;
		//displaying all the records of the file on to the screen

	 }	//end of for


	fin.close(); //closing the stream
	cout<<endl;
	getch();
}
  //*******************END OF FUNCTION*************************//
  //-----------------------------------------------------------//
  //*******************FUNCTION FOR SEARCHING******************//
void search()
{

	ifstream fin("teldiar.txt");
				//opening file for reading
	clrscr();
	char sname[30]; //temporary array for
			//name to be searched declared
	int d; //local variable `d' declared
	cout<<"\t\t\n\nEnter the name to be searched: ";
	cin.getline(sname,30);
	int flag = 0; //local variable `flag' initialized to 0

	for(int i = 1; i<=recno; i++)
	{

		fin.getline(username,30);
		fin.getline(phone,15);
		fin.getline(email,50);
		fin.getline(address,90);
		//reading from file


		d=strcmp(sname,username);//comparing ascii values
						//of all name present with
						//the name entered
		if(d==0) //i.e if the diff is=0(the names are same)
		{

		   flag=1;
		   //Displaying corresponding information for the name
		   cout<<"\n\t\tNAME		: "<<username<<endl;
		   cout<<"\t\tADDRESS	 : "<<address<<endl;
		   cout<<"\t\tPHONE NUMBER: "<<phone<<endl;
		   cout<<"\t\tEMAIL ID	: "<<email<<endl;
		   cout<<"\n\n\t\t\tTHANKYOU!!";
		   cout << endl;

		} //end of if
	}//end of for

		if(flag==0)
		{
		   cout<<"\n\t\t\tNAME NOT FOUND...PLEASE VERIFY!!";
		   //displaying error message
		}

	fin.close();   //closing the stream
	getch();
} //end of search

  //*****************END OF FUNCTION***************************//
  //-----------------------------------------------------------//
  //*************FUNCTION FOR MODIFYING RECORDS****************//
void modrec()
{
	 ofstream fout("temp.txt");
				//opening file for writing
	 ifstream fin("teldiar.txt");
				//opening file for reading

	 char sname[30];  //local array for
			  //name to be modified declared
	 int d,flag=0;	//local variables declared
	 char ch1;		//local char variable for choice declared
	 clrscr();
	 cout<<"\t\tEnter name for which the record is to be modified: ";
	 cin.getline(sname,30);

	 for(int i = 1; i <= recno; i++)
	 {

		fin.getline(username,30);
		fin.getline(phone,15);
		fin.getline(email,50);
		fin.getline(address,90);
		//reading from file

		d=strcmp(sname,username);//comparing the name entered
						//with all the names present
		if(d==0) //i.e if the names are same
		{
			flag=1;
			// Display original data
			cout<<"\t\tNAME:"<<username;
			cout<<"\n\t\tPHONE NUMBER:"<<phone;
			cout<<"\n\t\tADDRESS:"<<address;
			cout<<"\n\t\tEMAIL ID:"<<email;

			cout<<"\n\nDo you want to modify the record (Y/N): ";
			cin>>ch1;
			cin.get();

			// Entering new(modified) data from the user
			if(ch1=='y' || ch1 == 'Y')
			{
				cout<<"\n\t\tEnter name: ";
				cin.getline(username,30);
				cout<<"\t\tEnter new telephone number: ";
				cin.getline(phone,15);
				cout<<"\t\tEnter new address: ";
				cin.getline(address,90);
				cout<<"\t\tEnter changed e-mail id: ";
				cin.getline(email,50);

			} //end of if
		}//end of if(d==0)

		   fout<<username<<'\n'
			<<phone<<'\n'
			<<email<<'\n'
			<<address<<'\n';//writing data to file

	}  //end of for

	fin.close();
	fout.close(); 	//closing both the streams
	remove("teldiar.txt");
	rename("temp.txt","teldiar.txt");
	//removing old file(teldiar),and renaming the new file(temp)
	//to teldiar

	if(flag==0)
	{
			cout<<"\nNAME NOT FOUND ...PLEASE VERIFY";
	}	//end of if(flag==0)

}//end of modification

  //******************END OF FUNCTION**************************//
  //-----------------------------------------------------------//
  //******************FUNCTION FOR DELETING********************//
  void delrec()
  {
	ofstream fout("temp.txt");
				//opening file for writing
	ifstream fin("teldiar.txt");
				//opening file for reading

	clrscr();
	char ch;		//local variable for choice
	int temprec = recno;	//local variable declared

	for(int i = 1; i <= temprec; i++)
	{

		fin.getline(username,30);
		fin.getline(phone,15);
		fin.getline(email,50);
		fin.getline(address,90);
		//reading from file


		// Display original data
			cout<<"\t\tNAME:"<<username;
			cout<<"\n\t\tPHONE NUMBER:"<<phone;
			cout<<"\n\t\tADDRESS:"<<address;
			cout<<"\n\t\tEMAIL ID:"<<email;

		cout<<"\n\tDo you wish to delete this record?(Y/N)";
		cin>>ch;

		if(ch=='Y'|| ch=='y')
		{
			continue; // do not write to file
		}
		else
		{
			fout<<username<<'\n'
			<<phone<<'\n'
			<<email<<'\n'
			<<address<<'\n';//writing data to file
		}
	 }//end of while

	fout.close();
	fin.close();	 //closing both the streams
	remove("teldiar.txt");
	rename("temp.txt","teldiar.txt");
	//removing old file(teldiar),and renaming the new file(temp)
	//to teldiar

  }//end  of deleting

  //******************END OF FUNCTION**************************//
  //-----------------------------------------------------------//
  //**************FUNCTION FOR QUITTING************************//
  void quit()
  {
		clrscr();
		cout<<"\n\t\t\t\THANKYOU FOR VISITING!!";
		cout<<"\n\n\n\t\t\tPress any key...";
		//displaying message on the screen

		getch();

  }
  //******************END OF FUNCTION**************************//
  //-----------------------------------------------------------//
  //******************END OF PROGRAM***************************//




Is This A Good Question/Topic? 0
  • +

Replies To: plz rectify the errors in this code for a phonebook

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: plz rectify the errors in this code for a phonebook

Posted 14 November 2007 - 02:47 PM

that's a lot of lines, could you be specific about what and where the problem is?
Is it a compiler error, if so what line is it? or is it printing out wrong? if so what do you expect and what do you get?
Was This Post Helpful? 0
  • +
  • -

#3 ShinX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-November 07

Re: plz rectify the errors in this code for a phonebook

Posted 14 November 2007 - 03:28 PM

Hmmz I agree, you will need to narrow it down for someone to help you, or specify the line of code. I don't think its a error since you have stated it compiled just fine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1