lost_scott
August 4th, 2006, 01:02 PM
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <fstream>
#include <vector>
#include "shortphone.cpp"
#include "shortname.cpp"
#include "shortaddress.cpp"
using namespace std;
class Record{
shortName Name;
shortAddress Address;
shortPhone Phone;
public:
void AddNewRecord();
void ViewRecord();
void InFile();
void OutFile();
int FindRecord();
};
int menu()
{
int choice;
cout <<" Record List Processing System "<<endl;
cout <<"1. Add New Record "<<endl;
cout <<"2. Remove A Record Object from the list"<<endl;
cout <<"3. View all record object"<<endl;
cout <<"4. Find the object"<<endl;
cout <<"5. Exit"<<endl;
cin >> choice;
return choice;
}
void Read_Str(string str)
{
ofstream fout("info.dat", ios::binary|ios::app);
string data;
data = str;
int tmp = data.size();
fout.write(reinterpret_cast<char *>(&tmp),sizeof(int));
fout.write(reinterpret_cast<char *>(&data), sizeof(tmp) );
fout.write(data.c_str(), tmp+1);
fout.close();
}
void Record::InFile()
{
Read_Str(Name.getFirst());
Read_Str(Name.getLast());
Read_Str(Address.getCity());
Read_Str(Address.getID());
Read_Str(Address.getState());
Read_Str(Address.getStreet());
Read_Str(Address.getZip());
Read_Str(Phone.getArea());
Read_Str(Phone.getNumber());
Read_Str(Phone.getPrefix());
}
void Record::AddNewRecord()
{
cin.ignore();
string input;
cout << "First Name :";
getline(cin,input,'\n');
Name.setFirst(input);
cout << "Last Name :";
getline(cin,input,'\n');
Name.setLast(input);
cout << "ID: :"; //Address
getline(cin,input,'\n');
Address.setID(input);
cout << "Street :";
getline(cin,input,'\n');
Address.setStreet(input);
cout << "City :";
getline(cin,input,'\n');
Address.setCity(input);
cout << "State :";
getline(cin,input,'\n');
Address.setState(input);
cout << "Zip :";
getline(cin,input,'\n');
Address.setZip(input);
cout << "Area Code :";
getline(cin,input,'\n');
Phone.setArea(input);
cout << "Telephone - Prefix :";
getline(cin,input,'\n');
Phone.setPrefix(input);
cout << "Telephone - Numbers:";
getline(cin,input,'\n');
Phone.setNumber(input);
input.erase();
}
void Record::ViewRecord()
{
cout << Name.getFirst() <<" " << Name.getLast() <<" ";
cout << Address.getID() <<" " << Address.getCity() <<" " << Address.getStreet() <<" " << Address.getState() <<" " << Address.getZip() <<setw(4) ;
cout << Phone.getArea() <<" " <<Phone.getPrefix() <<"-" << Phone.getNumber() <<endl;
}
int Record::FindRecord()
{
cin.ignore();
string input,last_name;
int i = 1;
last_name = Name.getLast();
cout <<"Enter the last name :";
getline(cin, input);
i=last_name.compare(input);
return i;
}
typedef vector<Record> SetRecord;
int main()
{
SetRecord GrowingRecord;
for(;;){
int choice = menu();
switch(choice)
{
case(1):
{
Record Employee;
Employee.AddNewRecord();
Employee.InFile();
GrowingRecord.push_back (Employee);
break;
}
case(2):
{
int i,j;
cout <<"Index " << " Name " << setw(2) << " ID " << setw(2) << " Street " << setw(5) << " City " << setw(5) <<"State "<<setw(2) << " Telephone " <<endl;
for(i=0; i<GrowingRecord.size();i++)
{
Record Employee;
Employee = GrowingRecord[i];
cout << i << setw(2);
Employee.ViewRecord();
}
if(i==0){
cout <<"No Records Exist! "<<endl;
break;
}
cout << "Which record do you wish to delete (-1 to exit) ? (0-" << i-1 <<")" ;
cin >> j;
GrowingRecord.erase(GrowingRecord.begin()+j);
}
break;
case(3):
{
cout <<"Index " << " Name " << setw(2) << " ID " << setw(2) << " Street " << setw(5) << " City " << setw(5) <<"State "<<setw(2) << " Telephone " <<endl;
for(int i=0; i<GrowingRecord.size();i++)
{
Record Employee;
Employee = GrowingRecord[i];
cout << i << " ";
Employee.ViewRecord();
}
Record Employee;
break;
}
case(4):
{
int j;
for(int i=0; i<GrowingRecord.size();i++)
{
Record Employee;
Employee = GrowingRecord[i];
if((j=Employee.FindRecord()==0)){
cout <<"Found !"<< "The data is located at " << i << "th data" <<endl; break;}
else
cout <<"Not Found !";
}
break;
}
case(5):
exit(1); break;
default:
cout <<"Error" << endl;
};
}
return 0;
}
here is the code...i got no problems when reading out the classes into a binary file....but it prints junk or nth when i read it back
can anyone figure out the problem??? many thanks
#include <iomanip>
#include <string>
#include <cstdlib>
#include <fstream>
#include <vector>
#include "shortphone.cpp"
#include "shortname.cpp"
#include "shortaddress.cpp"
using namespace std;
class Record{
shortName Name;
shortAddress Address;
shortPhone Phone;
public:
void AddNewRecord();
void ViewRecord();
void InFile();
void OutFile();
int FindRecord();
};
int menu()
{
int choice;
cout <<" Record List Processing System "<<endl;
cout <<"1. Add New Record "<<endl;
cout <<"2. Remove A Record Object from the list"<<endl;
cout <<"3. View all record object"<<endl;
cout <<"4. Find the object"<<endl;
cout <<"5. Exit"<<endl;
cin >> choice;
return choice;
}
void Read_Str(string str)
{
ofstream fout("info.dat", ios::binary|ios::app);
string data;
data = str;
int tmp = data.size();
fout.write(reinterpret_cast<char *>(&tmp),sizeof(int));
fout.write(reinterpret_cast<char *>(&data), sizeof(tmp) );
fout.write(data.c_str(), tmp+1);
fout.close();
}
void Record::InFile()
{
Read_Str(Name.getFirst());
Read_Str(Name.getLast());
Read_Str(Address.getCity());
Read_Str(Address.getID());
Read_Str(Address.getState());
Read_Str(Address.getStreet());
Read_Str(Address.getZip());
Read_Str(Phone.getArea());
Read_Str(Phone.getNumber());
Read_Str(Phone.getPrefix());
}
void Record::AddNewRecord()
{
cin.ignore();
string input;
cout << "First Name :";
getline(cin,input,'\n');
Name.setFirst(input);
cout << "Last Name :";
getline(cin,input,'\n');
Name.setLast(input);
cout << "ID: :"; //Address
getline(cin,input,'\n');
Address.setID(input);
cout << "Street :";
getline(cin,input,'\n');
Address.setStreet(input);
cout << "City :";
getline(cin,input,'\n');
Address.setCity(input);
cout << "State :";
getline(cin,input,'\n');
Address.setState(input);
cout << "Zip :";
getline(cin,input,'\n');
Address.setZip(input);
cout << "Area Code :";
getline(cin,input,'\n');
Phone.setArea(input);
cout << "Telephone - Prefix :";
getline(cin,input,'\n');
Phone.setPrefix(input);
cout << "Telephone - Numbers:";
getline(cin,input,'\n');
Phone.setNumber(input);
input.erase();
}
void Record::ViewRecord()
{
cout << Name.getFirst() <<" " << Name.getLast() <<" ";
cout << Address.getID() <<" " << Address.getCity() <<" " << Address.getStreet() <<" " << Address.getState() <<" " << Address.getZip() <<setw(4) ;
cout << Phone.getArea() <<" " <<Phone.getPrefix() <<"-" << Phone.getNumber() <<endl;
}
int Record::FindRecord()
{
cin.ignore();
string input,last_name;
int i = 1;
last_name = Name.getLast();
cout <<"Enter the last name :";
getline(cin, input);
i=last_name.compare(input);
return i;
}
typedef vector<Record> SetRecord;
int main()
{
SetRecord GrowingRecord;
for(;;){
int choice = menu();
switch(choice)
{
case(1):
{
Record Employee;
Employee.AddNewRecord();
Employee.InFile();
GrowingRecord.push_back (Employee);
break;
}
case(2):
{
int i,j;
cout <<"Index " << " Name " << setw(2) << " ID " << setw(2) << " Street " << setw(5) << " City " << setw(5) <<"State "<<setw(2) << " Telephone " <<endl;
for(i=0; i<GrowingRecord.size();i++)
{
Record Employee;
Employee = GrowingRecord[i];
cout << i << setw(2);
Employee.ViewRecord();
}
if(i==0){
cout <<"No Records Exist! "<<endl;
break;
}
cout << "Which record do you wish to delete (-1 to exit) ? (0-" << i-1 <<")" ;
cin >> j;
GrowingRecord.erase(GrowingRecord.begin()+j);
}
break;
case(3):
{
cout <<"Index " << " Name " << setw(2) << " ID " << setw(2) << " Street " << setw(5) << " City " << setw(5) <<"State "<<setw(2) << " Telephone " <<endl;
for(int i=0; i<GrowingRecord.size();i++)
{
Record Employee;
Employee = GrowingRecord[i];
cout << i << " ";
Employee.ViewRecord();
}
Record Employee;
break;
}
case(4):
{
int j;
for(int i=0; i<GrowingRecord.size();i++)
{
Record Employee;
Employee = GrowingRecord[i];
if((j=Employee.FindRecord()==0)){
cout <<"Found !"<< "The data is located at " << i << "th data" <<endl; break;}
else
cout <<"Not Found !";
}
break;
}
case(5):
exit(1); break;
default:
cout <<"Error" << endl;
};
}
return 0;
}
here is the code...i got no problems when reading out the classes into a binary file....but it prints junk or nth when i read it back
can anyone figure out the problem??? many thanks