我要感谢您的大力支持。 i gotta承认,C++是徒劳的,令人惊讶,但有时是头疼。 一直在努力解决这一家务问题。 它几乎做了一些工作,但用一些艰难的时间来说明如何使用双亲档案。
问题如下:
You have been hired to help program a new "Ma Bell" telephone feature. The feature allows users with "PC" to obtain a disk of telephone numbers in addition to a telephone book. Your job is to write the program which will allow the phone information to be used. You must allow the addition of and viewing of phone number information.
The program should be able to: 1. Add people to the telephone list 2. View all people in the list 3. View based on just the last name
您可提出以下假设:
1. There are no more than 100 phone numbers 2. No name is longer than 50 characters. 3. Only one telephone number per person. 4. The address should be broken into only street # or PO Box street name zip code
撰写的法典一如下:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
class phonebook
{
private: char fname[25];
char lname[25];
char tel[10];
int pobox;
char street[20];
int zipcode;
int id;
public: phonebook(char[]=" ",char[]=" ",char[]=" ",int=0,char[]=" ",int=0,int=0);
void setfname(char*);
void setlname(char*);
void settel(char*);
char* getfname();
char* getlname();
char* gettel();
void setpobox(int);
void setstreet(char*);
void setzipcode(int);
int getpobox();
char* getstreet();
int getzipcode();
void setid(int);
int getid();
};
phonebook::phonebook(char* fn,char* ln, char* t,int p,char* s, int z,int i)
{
setfname(fn);
setlname(ln);
settel(t);
setpobox(p);
setstreet(s);
setzipcode(z);
setid(i);
}
void phonebook::setfname(char* fn){strcpy(fname,fn);}
void phonebook::setlname(char* ln){strcpy(lname,ln);}
void phonebook::settel(char* t){strcpy(tel,t);}
char* phonebook::getfname(){return fname;}
char* phonebook::getlname(){return lname;}
char* phonebook::gettel(){return tel;}
void phonebook::setpobox(int p){pobox=p;}
void phonebook::setstreet(char* s){strcpy(street,s);}
void phonebook::setzipcode(int z){zipcode=z;}
int phonebook::getpobox(){return pobox;}
char* phonebook::getstreet(){return street;}
int phonebook::getzipcode(){return zipcode;}
void phonebook::setid(int i){id=i;}
int phonebook::getid(){return id;}
int main()
{
phonebook pb;
fstream outfile("phonebook.dat",ios::in | ios::binary);
if(!outfile){cerr<<"file could not be created!";exit(1);}
//for(int i=0;i<100;i++)
//outfile.write(reinterpret_cast<const char*>(&pb),sizeof(phonebook));
char FN[25];
char LN[25];
char T[10];
int P;
char S[20];
int Z;
int x;
int id;
cout<<"Choose one of the following: "<<endl;
cout<<"1. Add people to the telephone list"<<endl;
cout<<"2. View all people in the list"<<endl;
cout<<"3. View based on just the last name"<<endl;
cin>>x;
switch(x)
{
case 1:
cout<<"Enter record number, first name, last name, telephone, pobox, street, zipcode: "<<endl;
cin>>id>>FN>>LN>>T>>P>>S>>Z;
pb.setid(id);
pb.setfname(FN);
pb.setlname(LN);
pb.settel(T);
pb.setpobox(P);
pb.setstreet(S);
pb.setzipcode(Z);
outfile.seekp((id-1)*sizeof(phonebook));
outfile.write(reinterpret_cast<const char*>(&pb),sizeof(pb));break;
case 2:
for(int i=0;i<100;i++){
outfile.seekg((i-1)*sizeof(phonebook));
cout<<pb.getfname()<<" "<<pb.getlname()<<" "<<pb.gettel()<<" "<<pb.getpobox()<<" "<<pb.getstreet()<<" "<<pb.getzipcode()<<endl;}break;
case 3:
cout<<"Enter last name: ";
cin>>LN;
outfile.seekp((id-1)*sizeof(phonebook));
outfile.read(reinterpret_cast<char*>(&pb),sizeof(pb));
if((strcmp(pb.getlname(),LN)==0))
cout<<pb.getfname()<<" "<<pb.getlname()<<" "<<pb.gettel()<<" "<<pb.getpobox()<<" "<<pb.getstreet()<<" "<<pb.getzipcode()<<endl;
else cout<<"name not found!"<<endl;break;
}
system("pause");
return 0;
}
when i want to list the info i added to the binary file, its not reading. I m only getting zeros. also i would like to know how to make the switch statement to ask me if i want to either add more phone numbers or like a loop for the switch cases. also, how can i make the program to search for a name inside the binary file. i tried my best but i just cant make it work.