【问题标题】:Passing value to a C++ function [duplicate]将值传递给 C++ 函数 [重复]
【发布时间】:2018-01-13 17:31:35
【问题描述】:

到目前为止,我一直在使用全局变量。我只记得这不是一个好习惯。那么,有什么办法可以改变这个吗?我应该将变量作为值还是引用传递?

这是代码 https://pastebin.com/JZaaR2Qd

using namespace std;

string user_name;
string str_age;
unsigned short int user_age;
char yes_no_prompt;

void user_biodata_input()
{
    cin.clear();
    cout << "Name : ";  getline(cin >> ws, user_name);
    cout << "Age : "; getline(cin, str_age); //taking age as a string
    stringstream(str_age) >> user_age ; //extract int from a string

    //Check if user input is not numeric, if so, repeat
    while (cin.fail()) {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Input invalid! Please re-enter your age in numeric..." << endl;
        cin >> user_age;
    }
}

int main()
{
    //Speed up cin and cout
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //Start
    cout << "Hi User, my name is Sirius. I'm your Digital Guidance (DG), nice to meet you..." << endl;
    cout << "Please provide your data..." << endl;
    user_biodata_input();

    show_user_biodata();

    while (yes_no_prompt == 'N' || yes_no_prompt == 'n')
    {
        cout << "Please re-enter your biodata..." << endl;
        user_biodata_input();
        show_user_biodata();
    }

【问题讨论】:

  • 当然,你可以通过引用。你有什么问题?

标签: c++ function c++11 pass-by-reference


【解决方案1】:

好吧,如果您打算使用 C++,您最好尝试利用语言功能,因此提供 User 类来封装您想要收集的用户数据的内部表示会更简洁。例如:

class User {

    public:
        User(string name, unsigned short int age):_name(name),_age(age) {}

        bool confirm() {
            cin.clear();
            char yes_no_prompt;
            cout << "Thank you for providing your data...";
            cout << "\nPlease confirm your data...(Y/N)\n" << endl;

            //printing border
            cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
            //printing student record
            cout << setfill(' ') << setw(1) << "|" << setw(15) << left << "Name" << setw(1) << "|" << setw(15) << left << "Age" << setw(1) << "|" << endl;
            //printing border
            cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
            //printing student record
            cout << setfill(' ') << setw(1) << "|" << setw(15) << left << this->_name << setw(1) << "|" << setw(15) << left << this->_age << setw(1) << "|" << endl;
            //printing border
            cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
            //printing student record

            cin >> yes_no_prompt;

            if (yes_no_prompt == 'Y' || yes_no_prompt == 'y')
            {
                cout << "\nThank you for giving cooperation...\nWe will now proceed to the academy..." << endl;
                return true;
            }
            return false;
        }
    private:
        string _name;
        unsigned short int _age;
};

现在你可以实现收集用户信息的功能了:

User createUser()
{
    cin.clear();
    string user_name;
    string str_age;
    unsigned short int age;
    cout << "Name : ";  getline(cin >> ws, user_name);
    cout << "Age : "; getline(cin, str_age); //taking age as a string
    stringstream(str_age) >> age ; //extract int from a string

    //Check if user input is not numeric, if so, repeat
    while (cin.fail()) {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Input invalid! Please re-enter your age in numeric..." << endl;
        cin >> age;
    }
    return User(user_name, age);
}

最后 main 看起来像这样:

int main()
{
    //Speed up cin and cout
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //Start
    cout << "Hi User, my name is Sirius. I'm your Digital Guidance (DG), nice to meet you..." << endl;
    cout << "Please provide your data..." << endl;

    User user = createUser();

    while (!user.confirm()) {
        user = createUser();

    }

    return 0;

}

当然这只是一个草案和建议,另一种选择是让你的函数都接收参数并能够返回值。虽然 OOP 更简洁地接近 IMO。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    • 2018-05-07
    相关资源
    最近更新 更多