I have a problem with search function by name ,could anyone help me?
This my code so far!!
Thanks in advance.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
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 firstname---------------------*/
void searchbyname(fstream &search_nameFile)
{
string name = getname( "Enter the Last name of the user " );
//int name = reinterpret_cast<int>(pname);
search_nameFile.seekg( ( name - 1 ) * sizeof( phonebook) );
phonebook person;
search_nameFile.read( reinterpret_cast<char *>( &person ),
sizeof( phonebook ) );
if ( person.getFirstName() != " " )
{
outputLine( cout, person );
search_nameFile.seekp( (name - 1 ) * sizeof( phonebook ) );
search_nameFile.write(
reinterpret_cast<const char *>( &person ),
sizeof( phonebook) );
}
else
cerr << "The name :" <<"["<< name
<<"]"<< " does not exist." << 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
/*-------------------------get name from the keyboard--------------------*/
string getname(string name)
{
do
{
cin >> name;
}while (name!="");
return name;
}
/*&-------------------------------------END------------------------------------*/