You are on page 1of 2

#include <fstream>

#include <iostream>
#include <ios>
#include <string>

using std::string;
using std::cout;
using std::endl;
using std::fstream;
using std::ios;

class Phonebook
{
public:
char FirstName [12];
char LastName [12];
char AreaCode [4];
char PhoneNumber[8];

public:
Phonebook(){};
// void operator [] (const Phonebook &PhoneNumber);
// void operator () (const Phonebook &AreaCode, &PhoneNumber);
// void operator = (const Phonebook &FirstName, const Phonebook &LastName,
const Phonebook &AreaCode, const Phonebook &PhoneNumber);

void print()const
{
cout << "Here is your array" << endl << endl;

cout << "Last" << '\t' << "First" << '\t' << "Phone number" << endl;
cout << LastName << '\t' << FirstName << '\t'
<< AreaCode << "-" << PhoneNumber<<endl;

cout << endl;


};
};

int main()
{
fstream book;
Phonebook array[5];
string input;

book.open("phonebook.txt", ios::in);
if (!book){
cout << "ERROR! opening phonebook.txt" << endl;
return -1; // so we don't print junk
}else {
for (int i=0; i< 5; i++){
getline(book, input);
strcpy(array[i].FirstName, input.c_str());
getline(book, input);
strcpy(array[i].LastName, input.c_str());
getline(book, input);
strcpy(array[i].AreaCode, input.c_str());

Made by raj shah 9464554250


getline(book, input);
strcpy(array[i].PhoneNumber, input.c_str());
}
}
array[0].print();
array[1].print();
array[2].print();
array[3].print();
array[4].print();

book.close();

return 0;
}

This is just a tossed together example to get you on your way. Once you can compile and run you can try
to get the progam to work the way you would like it to. I had to make up my own textfile and just put a
single entry on a line (then used getline). I just used strcpy because I am lazy and have to get back to
work.

Mark

Please, ask very specific questions on things you don't understand. It will help someone here actually be
helpful. I have reduced your class to just about nothing. Get the print funtion to work then you can
worry about the operators later.

Made by raj shah 9464554250

You might also like