I am trying to work on an assignment that allows you to create an entry for a phone book, but there are a few things confusing me:

#include <string>
#include <iostream>
#include "entry.h"

using namespace std;

entry::entry()
{
    p_fname = "";
    p_lname = "";
    p_phone = "";
}
entry::entry(std::string fname, std::string lname, std::string phone)
{
    p_fname = fname;
    p_lname = lname;
    p_phone = phone;
}


/// sets fname to its parameter
void entry::set_fname(std::string fname)
{
    p_fname = fname;
}
void entry::set_lname(std::string lname)
{
    p_lname = lname;
}
void entry::set_phone(std::string phone)
{
    p_phone = phone;
}
std::string entry::get_fname() const
{
    std::string fname;

    cout << "Enter First Name:" << endl;
    cin >> fname;
    return fname;
}
std::string entry::get_lname() const
{
    std::string lname;

    cout << "Enter Last Name:" << endl;
    cin >> lname;
    return lname;
}
std::string entry::get_phone() const
{
    std::string phone;

    cout << "Enter Phone Number:" << endl;
    cin >> phone;
    return phone;
}

And these are the headers of the last two functions that read an entry and print an entry, into and onto a stream, respectively. Can anyone tell me how to implement them?

/// reads from a stream into an entry. Needs to match up with operator<<
std::istream& operator>>(std::istream& in, entry&e);

/// 'prints' an entry onto a stream; hint: print each field, with a separator (a character that won't appear in the output otherwise, say : or ,)
std::ostream& operator<<(std::ostream& out, const entry&e);

Recommended Answers

All 2 Replies

std::istream& operator>>(std::istream& in, entry&e); is this line correct ? I mean what is the type of entry&e.

I'm supposing entry is a class. So when you are overloading >> operator, you can simply do it this way

 std::istream& operator>>(std::istream& in, entry &e)

 {

      cout<<"Enter first name :\n";

      in>>e.fname;

      cout<<"Enter last name : \n";

      in>>e.lname;

      cout<<"Enter phone number : \n ";

      in>>e.phone

      return in;

 }

Or else you can call the functions that you have defined like std::string entry::get_fname() const , std::string entry::get_lname() const and std::string entry::get_phone() const. Similarly do the same for output part.

Ok, if i were to usestd:: string entry::get_fname() const, do I need to add anything directly below it before moving to the next function?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.