我如何将其读入Entry fname?

时间:2019-03-22 21:22:31

标签: c++

我试图将fname lname和电话号码(电话号码)的用户条目读入私有类中相应命名的函数中。因为我不知道如何直接从输入流中读取它们(cin >> get_fname();),所以我不知道如何定义如何将它们读入正确的位置

老实说,我不知道该怎么做。

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;
}

1 个答案:

答案 0 :(得分:1)

您可以

公开class Entry中的变量并将用户输入加载到其中(您不再需要使用吸气剂)

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;
};

set_*variable*中定义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;
};

您使用get_*variable*函数返回已经设置的变量(我认为),因此这与实际设置这些变量无关。