Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <iostream>
#include <fstream>
#include <string.h>
#include <iomanip>
#include <conio.h>
using namespace std;

void clrscr() {
  #ifdef WINDOWS
  system("cls");
  #endif
  #ifdef LINUX
  system("clear");
  #endif
}

class phoneBook{
	char name[20],phno[15];
	public:
	void getdata();
	void showdata();
	char *getname(){ return name; }
	char *getphno(){ return phno; }
	void update(char *nm,char *telno){
		strcpy(name,nm);
		strcpy(phno,telno);
	}
};

void phoneBook :: getdata(){
	cout<<"\nEnter Name : ";
	cin>>name;
	cout<<"Enter Phone No. : ";
	cin>>phno;
}

void phoneBook :: showdata(){
	cout<<"\n";
	cout<<setw(20)<<name;
	cout<<setw(15)<<phno;
}


int main(){
	phoneBook rec;
	fstream file;
	file.open("d:\\phone.dat", ios::ate | ios::in | ios::out | ios::binary);
	char ch,nm[20],telno[6];
	int choice,found=0;
	while(1){
		clrscr();
		cout<<"\n*****Phone Book*****\n";
		cout<<"1) Add New Record\n";
		cout<<"2) Display All Records\n";
		cout<<"3) Search Telephone No.\n";
		cout<<"4) Search Person Name\n";
		cout<<"5) Update Telephone No.\n";
		cout<<"6) Exit\n";
		cout<<"Choose your choice : ";
		cin>>choice;
		switch(choice){
			case 1 : //New Record
				 rec.getdata();
				 cin.get(ch);
				 file.write((char *) &rec, sizeof(rec));
				 break;

			case 2 : //Display All Records
				 file.seekg(0,ios::beg);
				 cout<<"\n\nRecords in Phone Book\n";
				 while(file){
					file.read((char *) &rec, sizeof(rec));
					if(!file.eof())
						rec.showdata();
				 }
				 file.clear();
				 getch();
				 break;

			case 3 : //Search Tel. no. when person name is known.
				 cout<<"\n\nEnter Name : ";
				 cin>>nm;
				 file.seekg(0,ios::beg);
				 found=0;
				 while(file.read((char *) &rec, sizeof(rec)))
				 {
					if(strcmp(nm,rec.getname())==0)
					{
						found=1;
						rec.showdata();
					}
				 }
				 file.clear();
				 if(found==0)
					cout<<"\n\n---Record Not found---\n";
				 getch();
				 break;

			case 4 : //Search name on basis of tel. no
				 cout<<"\n\nEnter Telephone No : ";
				 cin>>telno;
				 file.seekg(0,ios::beg);
				 found=0;
				 while(file.read((char *) &rec, sizeof(rec)))
				 {
					if(strcmp(telno,rec.getphno())==0)
					{
						found=1;
						rec.showdata();
					}
				 }
				 file.clear();
				 if(found==0)
					cout<<"\n\n---Record Not found---\n";
				 getch();
				 break;

			case 5 : //Update Telephone No.
				 cout<<"\n\nEnter Name : ";
				 cin>>nm;
				 file.seekg(0,ios::beg);
				 found=0;
				 int cnt=0;
				 while(file.read((char *) &rec, sizeof(rec)))
				 {
					cnt++;
					if(strcmp(nm,rec.getname())==0)
					{
						found=1;
						break;
					}
				 }
				 file.clear();
				 if(found==0)
					cout<<"\n\n---Record Not found---\n";
				 else
				 {
					int location = (cnt-1) * sizeof(rec);
					cin.get(ch);
					if(file.eof())
						file.clear();

					cout<<"Enter New Telephone No : ";
					cin>>telno;
					file.seekp(location);
					rec.update(nm,telno);
					file.write((char *) &rec, sizeof(rec));
					file.flush();
				 }
				 break;
			case 6 : goto out;
		}
	}
out:
file.close();
return 0;
}
Posted
Updated 26-Aug-15 1:44am
v2

In case 5 you declare the integer variable cnt which is not allowed. It should be declared outside the switch. You should also not use a goto statement, especially not to break out of a switch within a while loop.
 
Share this answer
 
Comments
enhzflep 26-Aug-15 8:02am    
Actually, you can declare it there - but you can't initialize it, you have to set its value in a subsequent statement. That's also a good point you make with reference to the control mechanism used, I decided against it in the interests of simplicity, but see no need now that you've explained it succinctly. while(1) combined with a goto surely kills a seal, a baby or an angel somewhere. :laughs:
Richard MacCutchan 26-Aug-15 8:19am    
I couldn't remember the actual rule about declarations in switch statements; thanks for clarifying.
enhzflep 26-Aug-15 9:54am    
I only remember it because I made the mistake so many times in the past and uncovered it when changing my IDE and no longer having the -fpermissive compiler switch enabled by default. :laughs:
Oh, and you're welcome. :)
CPallini 26-Aug-15 8:25am    
5.
I would suggest a bit refactoring there, e.g.
C#
 //...
 bool terminate = false;
 while( ! terminate )
 {
   clrscr();
   show_menu();
   cin>>choice;
   switch(choice)
   {
     case 1 : //New Record
       rec.getdata();
       cin.get(ch);
       file.write((char *) &rec, sizeof(rec));
       break;

     case 2 : //Display All Records
       display_all_records(file);
       getchar();
       break;
     //... use helper functions also in cases 3,4,5
     case 6:
     default:
      terminate = true;
      break;
   } // end of switch
   file.close();
   return 0;
 } // end of main



// helper functions
  void show_menu()
  {
    cout<<"\n*****Phone Book*****\n";
    cout<<"1) Add New Record\n";
    cout<<"2) Display All Records\n";
    cout<<"3) Search Telephone No.\n";
    cout<<"4) Search Person Name\n";
    cout<<"5) Update Telephone No.\n";
    cout<<"6) Exit\n";
    cout<<"Choose your choice : ";
  }
  void display_all_records(fstream & file)
  {
    phoneBook rec;
    file.seekg(0,ios::beg);
    cout<<"\n\nRecords in Phone Book\n";
    while(file)
    {
      file.read((char *) &rec, sizeof(rec));
      if(!file.eof())
        rec.showdata();
    }
    file.clear();
  }
  //...
 
Share this answer
 
Comments
enhzflep 26-Aug-15 9:56am    
I always cry a little inside when I see a while(1) loop exited with a goto.
Nice job suggesting a re-factor and showing an example approach. A 5 from me.
CPallini 26-Aug-15 10:39am    
Thank you.
The problem is in your case 5: block.

If you change
int cnt=0;
to
int cnt;
cnt = 0;

the problem goes away. Basically, the compiler re-organizes your code so that the int cnt; declaration occurs before the switch statement.


The other way you can fix this is to enclose the entire contents of this particular case satement inside it's own block.

i.e
C++
case 5:
 ....
 ....
 ....
 break;

case 6:
 ....
 ....


becomes

C++
case 5:
 {
 ....
 ....
 ....
 }
 break;

case 6:
 ....
 ....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900