find specific data in binary file

#include <iostream>
#include <conio.h>
#include <iomanip>
#include <fstream>

using namespace std;

struct det
{
char state[20];
char city[20];
int postcode;
};

struct cuac
{
char name[20];
char address[50];
det address2;
int phonenum;
double balance;
char date[20];
};

long byteNum(int);
struct cuac write();
struct cuac read();
void showRec(cuac);

int main()
{
int ans;


cout <<"1.Open new Customer account."<<endl;
cout <<"2.Search & display customer record."<<endl;
cout <<"3.Search & delete customer account."<<endl;
cout <<"4.Search & change customer account."<<endl;
cout <<"5.Display the entire archive."<<endl;
cout <<"6.Exit."<<endl;
cout <<setprecision(2)<<showpoint<<fixed;
cin >> ans;

switch(ans)
{
case 1:write();
break;
case 2:read();
break;
}





getch();
return 0;
}


cuac write()
{
fstream dataFile;
dataFile.open("customer_account.dat",ios::binary | ios::out | ios::app);
cuac b;
system("cls");
cin.ignore();

cout <<"Enter the following fields of data:"<<endl;
cout <<"Plaease enter your name:";
cin.getline(b.name,20);
cout <<"Please enter your address:";
cin.getline(b.address,50);
cout <<"Further detail of address:"<<endl;

cout<<"State :";
cin.getline(b.address2.state,20);
cout<<"City :";
cin.getline(b.address2.city,20);
cout<<"Postcode :";
cin >>b.address2.postcode;
cout <<"Phone number :";
cin >>b.phonenum;
cout <<"Balance remaining :";
cin >>b.balance;
cin.ignore();
cout <<"Date of last payment :";
cin.getline(b.date,20);

dataFile.write(reinterpret_cast<char *>(&b),sizeof(b));
dataFile.close();
}

cuac read()
{
int number =0;
string name;
fstream dataFile;
dataFile.open("customer_account.dat",ios::in | ios::binary);

cuac b;
system("cls");
cout <<"Please enter the name :";
cin >> name;

while(!dataFile.eof())
{
dataFile.seekg(byteNum(number),ios::beg);
dataFile.read(reinterpret_cast<char *>(&b),sizeof(b));

if(b.name == name)
{
showRec(b);
break;
}
else
{
cout <<"Record still not found!"<<endl;
}
number +=1;
}


dataFile.close();
}


long byteNum(int recNum)
{
return sizeof(cuac) * recNum;
}

void showRec(cuac record)
{
cout <<"Name :";
cout <<record.name<<endl;
cout <<"Address :";
cout <<record.address<<endl;
cout <<"State :";
cout <<record.address2.state<<endl;
cout <<"City :";
cout <<record.address2.city<<endl;
cout <<"Postcode :";
cout <<record.address2.postcode<<endl;
cout <<"Phone Number :";
cout <<record.phonenum<<endl;
cout <<"Balance :";
cout <<record.balance <<endl;
cout <<"Date payment :";
cout <<record.date<<endl;
}


The second option of this program let me find the specific data in the customer_account.dat, but the result found nothing of the record i want even the data is existed when i entered in the first option.In contrary ,if i initialised the name with "Michael" like(string name = "Michael"),it reveal all the information of Michael correctly.Any correction of the codes ,so that i can find the record with cin the name i want ?
Topic archived. No new replies allowed.