-1

I am trying to read in a user entry for a fname lname and a phone number (number) into a function named accordingly in a private class. since i do not know how to read them directly from the input stream ( cin >> get_fname(); ) i do not know how i would go about defining how to get them read into the proper place

I honestly do not know how to go about this.

using namespace std;
string getResponse()
{
    string response;
    cout << endl << "What would you like to do?" << endl;
    cout << "Add and entry (a),";
    cout << "Sort list (s)," << endl;
    cout << "Print the list (p),";
    cout << "Find a name (f)," << endl;
    cout << "Quit (q)?";

    cin >> response;
    return response;
}

void printEntry(Entry entry)
{
    cout << entry.get_fname() << " " << entry.get_lname() << " " << entry.get_number() << endl;
}

void findName(const list<Entry>& data, string name)
{
    for (list<Entry>::const_iterator iter = data.begin(); iter != data.end(); iter++){
        if (name == iter->get_lname()){
            printEntry(*iter);
        }
    }
}



Entry readEntry()
{
    Entry entry;
    cout << "Please enter a first name: ";
    string fname;
    cin >> fname;
    entry.get_fname(fname);
    cout << "Please enter a last name: ";
    string lname;
    cin >> lname;
    entry.get_lname(lname);
    cout << "Please enter a phone number: ";
    string number;
    cin >> number;
    entry.get_number(number);

    return entry;
}

class Entry {
private:

    std::string fname;
    std::string lname;
    std::string number;
public:
    std::string get_fname() const;
    std::string get_lname() const;
    std::string get_number() const;


};

#endif // ENTRY_H_INCLUDED

#include "entry.h"

std::string Entry::get_fname() const {
    return fname;
}
std::string Entry::get_lname() const {
    return lname;
}
std::string Entry::get_number() const {
    return number;
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • 2
    May I recommend picking up [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) ? – Jesper Juhl Mar 22 '19 at 21:25
  • Hi, and welcome to SO. What exactly is the problem? Your code looks fine. –  Mar 22 '19 at 21:29
  • The `const` at the end of `std::string get_fname() const;` declares that `get_fname` is a `const` function. You can't use it to change the state of the object and can only use it to look . You will have to find a different path. Perhaps `readEntry` can be a `public` `static` method of `Entry` or a `friend` function? – user4581301 Mar 22 '19 at 21:30

1 Answers1

0

You can either

make the variables in class Entry public and load the user input into them (you don't need to have getters anymore)

Entry readEntry()
{
    Entry entry;
    cout << "Please enter a first name: ";
    cin >> entry.fname;

    cout << "Please enter a last name: ";
    cin >> entry.lname;

    cout << "Please enter a phone number: ";
    cin >> entry.number;

    return entry;
}
``````
class Entry {
public:
    std::string fname;
    std::string lname;
    std::string number;
};

or define set_*variable* functions in class Entry:

Entry readEntry()
{
    Entry entry;
    cout << "Please enter a first name: ";
    string fname;
    cin >> fname;
    entry.set_fname(fname);

    cout << "Please enter a last name: ";
    string lname;
    cin >> entry.lname;
    entry.set_lname(lname);

    cout << "Please enter a phone number: ";
    string number;
    cin >> entry.number;
    entry.set_number(number);

    return entry;
}
``````
class Entry {
public:
    std::string fname;
    std::string lname;
    std::string number;

    void set_fname(std::string _fname) { fname = _fname; };
    void set_lname(std::string _lname) { lname = _lname; };
    void set_number(std::string _number) { number = _number; };
private:
    std::string get_fname() const;
    std::string get_lname() const;
    std::string get_number() const;
};

You use the get_*variable* functions for returning the already set variables (I assume), so this has nothing to do with actually setting those variables.

Scamtex
  • 181
  • 1
  • 9