Hey all! I have this assignment for my school project to create a simple address book using classes that do the routine stuff like adding a contact, searching it, modifying it, deleting it...
Here is the code but it won't run properly. I tried but couldn't figure it out. It'd be a huge help for me if you guys could have a look at it and tell me what and where the problem is...
Thank you! :)

#include<fstream.h>
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<iomanip.h>
#include<process.h>
#include<ctype.h>
class contact
{ char fname[15];
  char lname[20];
  char r_add[20];
  char o_add[20];
  int phone, mobile, office_ph, fax;
  char mail_ID[15];
  public :
  int record;
  void enter();
  void display();
  void menu();
  void search();
  void modify();
  int no_of_records();
  void display_all();
} A;
void contact :: enter()
{ cout<<"Enter First Name : ";
  gets(fname);
  cout<<'\n'<<"Enter Last Name : ";
  gets(lname);
  cout<<'\n'<<"Enter Address (residence) : ";
  gets(r_add);
  cout<<'\n'<<"Enter Address (office) : ";
  gets(o_add);
  cout<<'\n'<<"Enter Phone No. (residence) : ";
  cin>>phone;
  cout<<'\n'<<"Enter Mobile No. : ";
  cin>>mobile;
  cout<<'\n'<<"Enter Phone No. (office) : ";
  cin>>office_ph;
  cout<<'\n'<<"Enter Fax No. : ";
  cin>>fax;
  cout<<'\n'<<"Enter E-Mail ID : ";
  gets(mail_ID);
  char ch;
  cin.get(ch);
}
void contact :: display()
{ clrscr();
  cout<<"Name : ";
  puts(fname);
  cout<<'\t';
  puts(lname);
  cout<<'\n'<<"Address (Residence) : ";
  puts(r_add);
  cout<<'\n'<<"Address (Office) : ";
  puts(o_add);
  cout<<'\n'<<"Phone (residence): "<<phone;
  cout<<'\n'<<"Mobile : "<<mobile;
  cout<<'\n'<<"Phone (office): "<<office_ph;
  cout<<'\n'<<"Fax : "<<fax;
  cout<<'\n'<<"E-Mail ID : ";
  puts(mail_ID);
}
int contact :: no_of_records()
{ ifstream fin;
  fin.open("ADDBOOK.DAT");
  fin.seekg(0, ios::end);
  int n;
  n = fin.tellg()/sizeof(A);
  cout<<'\n'<<"NUMBER OF RECORDS = "<<n ;
  return n;
}
void contact :: menu()
{ clrscr();
  int ch, ch1;
  do { clrscr();
  cout<<"\t\t\t"<<"*****  MAIN MENU  *****";
  cout<<'\n'<<"1.Add contact(s)";
  cout<<'\n'<<"2.Display all contacts";
  cout<<'\n'<<"3.Modify";
  cout<<'\n'<<"4.Search";
  cout<<'\n'<<"5.EXIT";
  cout<<'\n'<<'\n'<<"Enter your choice : ";
  cin>>ch;
  switch(ch)
  { case 1 : clrscr();
	     fstream fout;
	     fout.open("ADDBOOK.DAT", ios::out);
	     char x = 'y';
	     while (x == 'y' || x == 'Y')
	     { A.enter();
	       fout.write((char*)& A, sizeof (A));
	       cout<<'\n'<<"Enter more contacts (Y/N)... ";
	       cin>>x;
	     }
		      fout.close();
	     break;
    case 2 : clrscr();
	     fstream fin;
	     fin.open("ADDBOOK.DAT", ios::in | ios::app);
	     int z = no_of_records();
	     for(int i=0; i<z; i++)
	     { fin.read((char*)& A, sizeof(A));
	       A.display();
	     }
	     fin.close();
	     getch();
	     break;
    case 3 : clrscr();
	     A.modify();
	     break;

    case 4 : clrscr();
	     A.search();
	     break;

    case 5 : clrscr();
	    cout<<'\n'<<'\t'<<" ********   EXITING...   ********";
	    exit(1);
	    break;
}  }while (ch1!=5); }
void contact :: modify()
{  int l=0,flag=0;
	fstream infile,outfile;
	char fname1[20];
	char lname1[20];
	int ch2;
	do
	{  int i; clrscr();
		cout<<"\nMODIFY";
		cout<<"\n\n1.RESIDENCIAL ADDRESS";
		cout<<"\n\n2.OFFICIAL ADDRESS";
		cout<<"\n\n3.RESIDENCTIAL TELEPHONE NUMBER";
		cout<<"\n\n4.OFFICIAL TELEPHONE NUMBER";
		cout<<"\n\n5.MOBILE NUMBER";
		cout<<"\n\n6.FAX NO";
		cout<<"\n\n7.EMAIL ID";
		cout<<"\n\n8.EXIT";
		cout<<"\n\nENTER YOUR CHOICE:";
		cin>>ch2;

		//switch....
		switch(ch2)
		{
			case 1:

				//MODIFY RESIDENCIAL ADDRESS

				cout<<"\nENTER FIRST NAME:";gets(fname1);
				gets(fname1);

				l=strlen(fname1);

				for(i=0;i<l;i++)
				{ fname1[i]=toupper(fname1[i]); }

				cout<<"\nENTER LAST NAME:";gets(lname1);

				l=strlen(lname1);

				for(i=0;i<l;i++)
				{lname[i]=toupper(lname1[i]);}

				infile.open("ADDBOOK.DAT", ios::in|ios::beg);
				outfile.open("temp.dat",ios::out);

				if(!infile)
				{
					cout<<"\nERROR";
					exit(0);
				}
				while((!infile.eof())&&(flag==0))
				{
					infile.read((char*)& A, sizeof (A));
					if((strcmp(fname,fname1)==0) && (strcmp(lname,lname1)==0))
					{
						cout<<"\nENTER THE NEW ADDRESS";
						gets(r_add);

						outfile.write((char*)& A, sizeof (A));
						flag=1;
					}
					else

						outfile.write((char*)& A,sizeof (A));
				}
				infile.close();
				outfile.close();
				remove("ADDBOOK.DAT");
				rename("temp.dat", "ADDBOOK.DAT");
				break;

			case 2:

				//MODIFY OFFICIAL ADDRESS
				cout<<"\nENTER FIRST NAME:";gets(fname1);

				l=strlen(fname1);

				for(i=0;i<l;i++)
				{fname1[i]=toupper(fname1[i]);}

				cout<<"\nENTER LAST NAME:";gets(lname1);
				l=strlen(lname1);

				for(i=0;i<l;i++)
				{lname1[i]=toupper(lname1[i]);}

				infile.open("ADDBOOK.DAT", ios::in|ios::beg);
				outfile.open("temp.dat",ios::out);
				while(!infile)
				{
					cout<<"\nERROR";
					exit(0);
				}

				while((!infile.eof())&&(flag==0))
				{
					infile.read((char*)& A,sizeof (A));
					if((strcmp(fname,fname1)==0) && (strcmp(lname,lname1)==0))
					{
						cout<<"\nENTER THE NEW ADDRESS";
						gets(o_add);

						outfile.write((char*)& A,sizeof (A));
						flag=1;
					}
					else
						outfile.write((char*)& A,sizeof (A));
				}
				infile.close();
				outfile.close();
				remove("ADDBOOK.DAT");
				rename("temp.dat","ADDBOOK.DAT");
				break;

			case 3:
				//MODIFY RESIDENCTIAL TELEPHONE NUMBER

				cout<<"\nENTER FIRST NAME:";gets(fname1);

				l=strlen(fname1);

				for(i=0;i<l;i++)
				{fname1[i]=toupper(fname1[i]);};

				cout<<"\nENTER LAST NAME:";gets(lname1);
				l=strlen(lname1);

				for(i=0;i<l;i++)
				{lname1[i]=toupper(lname1[i]);}

				infile.open("ADDBOOK.DAT", ios::in|ios::beg);
				outfile.open("temp.dat",ios::out);
				while(!infile)
				{
					cout<<"\nERROR";
					exit(0);
				}

				while((!infile.eof())&&(flag==0))
				{
					infile.read((char*)& A,sizeof (A));
					if((strcmp(fname,fname1)==0) && (strcmp(lname,lname1)==0))
					{
						cout<<"\nENTER THE NEW TELEPHONE NUMBER";
						cin>>phone;
						outfile.write((char*)& A,sizeof (A));
						flag=1;
					}
					else
						outfile.write((char*)& A,sizeof (A));
				}

				infile.close();
				outfile.close();
				remove("ADDBOOK.DAT");
				rename("temp.dat","ADDBOOK.DAT");
				break;

			case 4:
				//MODIFY OFFICIAL TELEPHONE NUMBER

				cout<<"\nENTER FIRST NAME:";gets(fname1);
				l=strlen(fname1);

				for(i=0;i<l;i++)
				{fname1[i]=toupper(fname1[i]);}

				cout<<"\nENTER LAST NAME:";gets(lname1);
				l=strlen(lname1);
				for(i=0;i<l;i++)
				{lname[i]=toupper(lname1[i]);}

				infile.open("ADDBOOK.DAT",ios::in|ios::beg);
				outfile.open("temp.dat",ios::out);
				while(!infile)
				{
					cout<<"\nERROR";
					exit(0);
				}
				while((!infile.eof())&&(flag==0))
				{
					infile.read((char*)& A,sizeof (A));
					if((strcmp(fname,fname1)==0) && (strcmp(lname,lname1)==0))
					{
						cout<<"\nENTER THE NEW TELEPHONE NUMBER";
						cin>>office_ph;
						outfile.write((char*)& A,sizeof (A));
						flag=1;
					}
					else
						outfile.write((char*)& A,sizeof(A));
				}
				infile.close();
				outfile.close();
				remove("ADDBOOK.DAT");
				rename("temp.dat","ADDBOOK.DAT");
				break;

			case 5:

				//MODIFY MOBILE NUMBER

				cout<<"\nENTER FIRST NAME:";gets(fname1);
				l=strlen(fname1);
				for(i=0;i<l;i++)
				{fname1[i]=toupper(fname1[i]);}

				cout<<"\nENTER LAST NAME:";gets(lname1);
				l=strlen(lname1);
				for(i=0;i<l;i++)
				{lname1[i]=toupper(lname1[i]);}

				infile.open("ADDBOOK.DAT", ios::in|ios::beg);
				outfile.open("temp.dat",ios::out);
				while(!infile)
				{
					cout<<"\nERROR";
					exit(0);
				}
				while((!infile.eof())&&(flag==0))
				{
					infile.read((char*)& A,sizeof (A));
					if((strcmp(fname,fname1)==0) && (strcmp(lname,lname1)==0))
					{
						cout<<"\nENTER THE NEW TELEPHONE NUMBER";
						cin>>mobile;
						outfile.write((char*)& A, sizeof (A));
						flag=1;
					}
					else
					outfile.write((char*)& A,sizeof (A));
				}
				infile.close();
				outfile.close();
				remove("ADDBOOK.DAT");
				rename("temp.dat","ADDBOOK.DAT");
				break;

			case 6:

				//MODIFY FAX NUMBER

				cout<<"\nENTER FIRST NAME:";gets(fname1);
				l=strlen(fname1);

				for(i=0;i<l;i++)
				{fname1[i]=toupper(fname1[i]);}

				cout<<"\nENTER LAST NAME:";gets(lname1);
				l=strlen(lname1);
				for(i=0;i<l;i++)
				{lname1[i]=toupper(lname1[i]);}

				infile.open("ADDBOOK.DAT",ios::in|ios::beg);
				outfile.open("temp.dat",ios::out);
				while(!infile)
				{
					cout<<"\nERROR";
					exit(0);
				}

				while((!infile.eof())&&(flag==0))
				{
					infile.read((char*)& A,sizeof (A));
					if((strcmp(fname,fname1)==0) && (strcmp(lname,lname1)==0))
					{
						cout<<"\nENTER THE NEW TELEPHONE NUMBER";
						cin>>fax;
						outfile.write((char*)& A, sizeof (A));
						flag=1;
					}
					else
					outfile.write((char*)& A,sizeof (A));
				}
				infile.close();
				outfile.close();
				remove("ADDBOOK.DAT");
				rename("temp.dat","ADDBOOK.DAT");
				break;

			case 7:
				//MODIFY THE EMAIL ADDRESS
				cout<<"\nENTER FIRST NAME:";gets(fname1);
				l=strlen(fname1);

				for(i=0;i<l;i++)
				{fname1[i]=toupper(fname1[i]);}

				cout<<"\nENTER LAST NAME:";gets(lname1);
				l=strlen(lname1);
				for(i=0;i<l;i++)
				{lname1[i]=toupper(lname1[i]);}

				infile.open("ADDBOOK.DAT", ios::in|ios::beg);
				outfile.open("temp.dat",ios::out);
				while(!infile)
				{
					cout<<"\nERROR";
					exit(0);
				}

				while((!infile.eof())&&(flag==0))
				{
					infile.read((char*)& A,sizeof (A));
					if((strcmp(fname,fname1)==0) && (strcmp(lname,lname1)==0))
					{
						cout<<"\nENTER THE NEW EMAIL ID:";
						gets(mail_ID);
						outfile.write((char*)& A,sizeof (A));
						flag=1;
					}
					else
					 outfile.write((char*)& A,sizeof (A));
				}
				infile.close();
				outfile.close();
				remove("ADDBOOK.DAT");
				rename("temp.dat","ADDBOOK.DAT");
				break;

				} }	while(ch2!=8);
}

//FUNCTION TO SEARCH FOR A CONTACT
void contact :: search()
{
	int l;
	ifstream infile;
	char fname1[20];
	char lname1[20];
	cout<<"\nENTER FIRST NAME:";
	gets(fname1);


	l=strlen(fname1);

	for(int i=0;i<l;i++)
		{ fname1[i]=toupper(fname1[i]); }

	cout<<"\nENTER LAST NAME:";
	gets(lname1);

	int counter;
	l=strlen(lname1);

	for(i=0;i<l;i++)
		{ lname1[i]=toupper(lname1[i]); }

	infile.open("ADDBOOK.DAT",ios::in);
	if(!infile)
	{
		cout<<"\nERROR";
		exit(0);
	}
	infile.seekg(0,ios::beg);
	infile.read((char*)& A, sizeof (A));
	while (!infile.eof())
	{
		if((strcmp(fname,fname1)==0) )
		{
			clrscr();
			++counter;
			A.display();
			getch();
		}
		infile.read((char*)& A, sizeof (A));

	}
	infile.close();
	cout<<"\nTHE NUMBER OF CONTACTS FOUND="<<counter;


 }

int login ()
{	
	char loginID[8],ch;
	char loginID1[]="preeti";
	int i=0,k=0;
	char a,b[8],c[]="everest";
	cout<<"\n\n\n\n\n\n\n\n\n\n";
	cout<<"***************************************************************";
	cout<<"\n\nEnter LoginID:";
	gets(loginID);
	cout<<"\n\nEnter password:";
	fflush(stdin);
		do
				{
				ch=getch();
				if(isalnum(ch))
					{
					b[i]=ch;
					cout<<"*";
					i++;
					}
				else
					if(ch=='\r')
						b[i]='\0';
					else if(ch=='\b')
						{
						i--;
						cout<<"\b\b";
						}
				}
			while(ch!='\r');

	b[i]='\0';
	fflush(stdin);
	cout<<"\n";
	cout<<"************************************************************";
	cout<<"\n\n\n\n\n\n";
	getch();
	if((strcmp(b,c)==0)&&(strcmp(loginID1,loginID)==0))
			{
					 cout<<"\n\n\nYou have entered successfully\n\n\n";
					 getch();
					return(1);
			}
	else
			{
					k++;
					cout<<"\tSorry!!! Wrong  ID or Password";
					cout<<"\n\n\n";
					return(0);

			}
}

void main()
{
	clrscr();
	int val,ch;
	a: val=login();
	if (val==0)
	{
		cout<<"WOULD YOU LIKE TO \n";
		cout<<"\t1.TRY AGAIN ";
		cout<<"\n\t2.EXIT";
		cout<<"\n\nENTER YOUR CHOICE : ";
		cin>>ch;
		if (ch==1)
			goto a;
		else
			exit(0);
	}
	A.menu();
}

void main() must be int main().

Why are you using C's fgets() instead of c++ getline() ? Does't make sense to use those C functions in a C++ program.

Example: delete stdio.h, string.h and conio.h from your program as they are not needed.

void contact :: enter()
{ cout<<"Enter First Name : ";
  cin.getline(fname, sizeof(fname));
  cout<<'\n'<<"Enter Last Name : ";
  cin.getline(lname, sizeof(lname));
  cout<<'\n'<<"Enter Address (residence) : ";
  cin.getline(r_add, sizeof(r_add));
  cout<<'\n'<<"Enter Address (office) : ";
  cin.getline(o_add, sizeof(o_add));
  cout<<'\n'<<"Enter Phone No. (residence) : ";
  cin>>phone;
  cout<<'\n'<<"Enter Mobile No. : ";
  cin>>mobile;
  cout<<'\n'<<"Enter Phone No. (office) : ";
  cin>>office_ph;
  cout<<'\n'<<"Enter Fax No. : ";
  cin>>fax;
  cout<<'\n'<<"Enter E-Mail ID : ";
  cin.getline(mail_ID, sizeof(mail_ID));
  cin.get();
}

Hey all! I have this assignment for my school project to create a simple address book using classes that do the routine stuff like adding a contact, searching it, modifying it, deleting it...
Here is the code but it won't run properly. I tried but couldn't figure it out. It'd be a huge help for me if you guys could have a look at it and tell me what and where the problem is...
Thank you! :)

Do you think it is easy for anyone to try and help you when you write stuff like 'it won't run properly' or 'i tried but couldn't figure it out' ?? If you tried you should write specifically what you tried so that we know. And what does it mean by 'it won't run properly' ? Which part works? Which part doesn't work? Am i supposed to go through your entire code and understand that? Don't you think giving these details are important? You're lucky AD has given you an answer, normally such posts will get no replies at all and rightly so.

Uh, sorry...
The modification and the searching functions won't work. They take in the name but that's it...no displaying of the data.
Further, the display all function is displaying only the last record.
Further, the main function isn't returning any value so why should it be int and not void?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.