Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Phone book


  • Please log in to reply

#1
mozala

mozala

    New Member

  • Member
  • Pip
  • 8 posts
Hi can anybody help me with this code!!
Im trying to do phone book program but i run it it shows non ending writtings on the screen and i dont know whats going on can u help me?It doesnt exactly do what i want please help..
Thanks
Mozala
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
#pragma warning(disable: 4786)
#include <vector>
using namespace std;


class phonebook {

	public :	 

  phonebook() : MobilNumber(0), lastName(""), firstName(""), pcode(0),Email(""),city(""),Street("") { }
  void setMobilNumber( int num) { MobilNumber = num; }
  void setLastName (string lname) { lastName = lname; }
  void setFirstName(string fname) { firstName = fname; }
  void setEmail( string mail) { Email = mail;}
  void setcity (string cty) { city = cty;}
  void setstreet(string strt) { Street = strt; }
  void setpcode( int code) {  pcode = code; }
  int getMobilNumber() const { return MobilNumber; }
  string getLastName() const { return lastName; }
  string getFirstName() const { return firstName; }
  int getpcode() const { return pcode; }
  string getEmail() const { return Email; }
  string getcity() const { return city; }
  string getstreet() const { return Street; }
  
private:

		string firstName;
		string lastName;
		int MobilNumber;
		string Email;
		string city;
		string Street;
		int pcode;
};


			int enterChoice();
			void textFile( fstream& );
			void updateRecord( fstream& );
			void newRecord( fstream& );
			void deleteRecord( fstream& );
			void outputLine( ostream&, const phonebook& );
			int getmobil( const char * const );
			char getname( const char * const );
			void searchbyname(fstream&);
			void searchbyphone(fstream&);
			enum Choices { TEXTFILE = 1, UPDATE, NEW, DELETE,SEARCH_BY_NAME,SEARCH_BY_PHONE,END};
	
/*-----------------------------------Main function------------------------------*/
int main()
{
	  // note: fstream object, both input and output
  fstream inOutUser( "credit.dat", ios::in | ios::out );

   if ( !inOutUser )
   {  cerr << "File could not be opened." << endl;
	  
   }
   
   int choice;
		  
   while ( ( choice = enterChoice() )!=END  )
   {
	  switch ( choice )
	  {
		 case TEXTFILE:					  // create a text file to print
			textFile( inOutUser );
			break;
		 case UPDATE:						// update a record
			updateRecord( inOutUser );	 
			break;
		 case NEW:						   // add a record
			newRecord( inOutUser );		
			break;
		 case DELETE:						// remove a record
			deleteRecord( inOutUser );
			break;
		 case SEARCH_BY_NAME:
			 searchbyname(inOutUser);
			 break;
		 case SEARCH_BY_PHONE:
			 searchbyphone(inOutUser);
			 break;
		 default:
			cerr << "Incorrect choice\n";
			 break;
	  }
	  inOutUser.clear();  // resets end-of-file indicator
   }
   
   return 0;
}
/*-----------------------------------User's choice------------------------------*/
// Prompt for and input menu choice
int  enterChoice()
{
   cout << "\nEnter your choice" << endl
		<< "1 - store a formatted text file of \n"
		<< "	called \"print.txt\" for printing\n"
		<< "2 - update a record\n"
		<< "3 - add a new record\n"
		<< "4 - delete a record\n"
		<< "5 - search by name\n"
		<< "6 - search by phone\n"
		<< "7 - end program\n? ";

   int menuChoice;
   cin >> menuChoice;
   return menuChoice;
}
/*-----------------------------------Creation of textfile------------------------------*/
// Create formatted text file for printing
void textFile( fstream &readFromFile )
{
   ofstream outPrintFile( "c:\\print.txt", ios::out );

   if ( !outPrintFile )
   {  cerr << "Print file could not be opened." << endl;
	  exit(1);
   }
		
   outPrintFile << setiosflags( ios::left ) << setw( 10 ) 
	   << "First name :" << setw( 16 ) << "Last Name :" << setw( 11 )
	   << "Email address :"<<setw(10)
	   << "City name :" <<setw(10)<<"Street name :"<<setw(10)
	   << "Postal code :"<<resetiosflags( ios::left ) 
	   << setw( 10 ) << "Mobil number" << endl;
   readFromFile.seekg( 0 );	  // start at the beginning
   phonebook person;
   readFromFile.read( reinterpret_cast<char *>( &person ),
					  sizeof( phonebook) );

   while ( !readFromFile.eof() )
   {
	  if ( person.getMobilNumber() != 0 )		  // skip empty records
		 outputLine( outPrintFile, person);

	  readFromFile.read( reinterpret_cast<char *>( &person ), 
						 sizeof(phonebook ) );
   }
  
}
/*-----------------------------------UpdateRecord function------------------------------*/

// Update mobil's number
void updateRecord( fstream &updateFile )
{
   int mobil = getmobil( "Enter Mobil number to update" );

   updateFile.seekg( ( mobil - 1 ) * sizeof( phonebook) );

  phonebook person;
   updateFile.read( reinterpret_cast<char *>( &person ), 
					sizeof( phonebook ) );

   if ( person.getMobilNumber() != 0 )
   {
	  outputLine( cout, person );
	  
	  updateFile.seekp( ( mobil - 1 ) * sizeof( phonebook ) );
	  updateFile.write(
	  reinterpret_cast<const char *>( &person ),
	  sizeof( phonebook) );
   }
   else
	  cerr << "Mobil #" << mobil 
		   << " has no information." << endl; 
}
/*-----------------------------------New Users entryfile function------------------------------*/

// Create and insert new record
void newRecord( fstream &insertInFile )
{
   int mobil= getmobil( "Enter new mobil number" );

   insertInFile.seekg( (mobil - 1 ) * sizeof( phonebook) );

 phonebook person;
   insertInFile.read( reinterpret_cast<char *>( &person ), 
					  sizeof(phonebook ) );
	  string firstName;
	  string lastName;
	  string Email;
	  string city;
	  string Street;
		int pcode;
   if ( person.getMobilNumber() == 0 )	// make sure record is empty
   {
	  cout << "Enter lastname, firstname,mobilnumber,postal code,Email address,city,street\n ";
	  cin >> lastName  >> firstName 
		  >> pcode  >> Email
		  >> city >>  Street;
		person.setLastName( lastName );
		person.setFirstName( firstName );
		person.setpcode( pcode );
		person.setMobilNumber( mobil );
		person.setcity( city );
		person.setstreet( Street );
		person.setEmail( Email);
	   
	  insertInFile.seekp( ( mobil - 1 ) * 
						  sizeof( phonebook ) );
	  insertInFile.write( 
		 reinterpret_cast<const char *>( &person ), 
		 sizeof(phonebook ) );
   }
   else
	  cerr << "mobilnumber #" << mobil
		   << " already contains information." << endl;
}
 
 
/*-----------------------------------Deletion of User's details------------------------------*/

// Delete an existing record
void deleteRecord( fstream &deleteFromFile )
{
   int mobil = getmobil( "Enter mobil number to delete" );

   deleteFromFile.seekg( (mobil - 1) * sizeof( phonebook ) );

  phonebook person;
   deleteFromFile.read( reinterpret_cast<char *>( &person ), 
						sizeof( phonebook ) );

   if ( person.getMobilNumber() != 0 )	   // make sure record exists
   {
   phonebook * MobilOwner = new phonebook();

	  deleteFromFile.seekp( ( mobil - 1) * 
							sizeof( phonebook ) );
	  deleteFromFile.write( 
		 reinterpret_cast<const char *>( &MobilOwner ), 
		 sizeof( phonebook ) );
	 
	  cout << "Mobil #" << mobil << " deleted." << endl;
   }
   else
	  cerr << "Mobil #" << mobil << " is empty." << endl;
}
/*-----------------------------------User's Output function-----------------------------------------*/

// Output a line of User's information
void outputLine( ostream &output, const phonebook &c )
{
   output << setiosflags( ios::left ) << setw( 10 ) 
		  << c.getMobilNumber() << setw( 16 ) << c.getLastName() 
		  << setw( 11 ) << c.getFirstName() << setw( 10 ) 
		  << c.getEmail() <<setw( 10 ) << c.getcity() <<setw( 10 )
		  << c.getstreet() <<setw( 10 ) << c.getpcode() <<setw( 10 )
		  << setprecision( 2 ) << resetiosflags( ios::left )
		  << setiosflags( ios::fixed | ios::showpoint ) ;		
   
}

/*-----------------------------------Search User by the name-----------------------------------------*/
void searchbyname(fstream &search_nameFile)

{
	 cout<<"An empty string or EOF will stop the program"<<endl;


			for (;;)
	{
			string name;
			cout<<"Enter the name";
			  if ( !getline ( cin, name ) || name.empty() )
			  break;

			 if ( !search_nameFile)
		   
			 {
				cerr<<"Error opening file"<<endl;
	 		 }

			  vector<string> lines;
			  string line;

			  while ( getline ( search_nameFile, line ) )
	   
	   {
		  if ( line.find ( name ) )
		  lines.push_back ( line );
	   }

		  if ( lines.empty() )
		  cout<<"No records found"<<endl;
		  else if ( lines.size() == 1 )
		  cout<<"One record found"<<endl;
		  else
		  cout<<"Multiple records found"<<endl;

		  for ( vector<string>::size_type i = 0; i < lines.size(); i++ )
		  cout<< lines[i] <<endl;
	}



}

/*-----------------------------------Search by the phone-----------------------------------------*/
void  searchbyphone(fstream &search_phoneFile)
{
   int mobil = getmobil( "Enter Mobil number you want to search by" );

   search_phoneFile.seekg( ( mobil - 1 ) * sizeof( phonebook) );

  phonebook person;
  search_phoneFile.read( reinterpret_cast<char *>( &person ), 
					sizeof( phonebook ) );

   if ( person.getMobilNumber() != 0 )
   {
	  outputLine( cout, person );
	  
	  search_phoneFile.seekp( ( mobil - 1 ) * sizeof( phonebook ) );
	 search_phoneFile.write(
	  reinterpret_cast<const char *>( &person ),
	  sizeof( phonebook) );
   }
   else
	  cerr << "Mobil #" <<"["<< mobil
		   <<"]"<< " does not exist." << endl; 
} 
/*-----------------------------------get mobil number from the keyboard-----------------------------------------*/
int getmobil( const char * const prompt )
{
   int mobil;

   do {
	  cout << prompt << " (1 - 100): ";
	  cin >> mobil;
   } while ( mobil < 1 || mobil > 100 );

   return mobil;
} // end getmobil



  • 0

Advertisements







Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP