简体   繁体   English

C++ 有输入到类

[英]C++ having input to class

I made 2 function : getdata, print for input/print variable in person class我做了 2 个函数:getdata,在person类中打印输入/打印变量

    #include <iostream>
    #include <string>

    using namespace std;

    class person {
          public:
               char name[19];
               int born_year, married_year;
    };

    void getdata(char* name, int born_year,int married_year) {
         cout << "Enter Name!\n > ";
         cin.getline(name, 19);                         
         cout << "What's the Born Year?\n > ";
         cin >> born_year;
         cout << "What's the Married Year?\n > ";
         cin >> married_year;
    }

    void print(char* name, int born_year, int married_year) {
         cout << "Your Name : " << *name;
         cout << "\n Your Born Year : " << born_year;
         cout << "\n Your Married Year : " << married_year;
    }


    int main(void) {
        person p;
        int born_year, married_year;
        getdata(p.name, p.born_year, married_year);
        print(p.name, p.born_year, p.married_year);
        return 0;
    }

I'm trying to have input in a function outside the class我正在尝试在课外的函数中输入

(class only have variables about person) (类只有关于人的变量)

and print with with other function.并用其他功能打印。 How could I make this work?我怎么能让这个工作?

Everything looks fine.一切看起来都很好。 Just you need to call 'function by reference' to directly change values of class variable.只是您需要调用“引用函数”来直接更改类变量的值。 https://www.tutorialspoint.com/cplusplus/cpp_function_call_by_reference.htm https://www.tutorialspoint.com/cplusplus/cpp_function_call_by_reference.htm

void getdata(char* name, int born_year,int married_year)

you can remove variables declared in main function and use p.您可以删除在 main 函数中声明的变量并使用 p。 before every parameter.在每个参数之前。

getdata(p.name, p.born_year, p.married_year);

Your problem is in the variable types to your getdata function.您的问题在于getdata函数的变量类型。 The integer parameters need to be pass-by-reference (either pointers or references, the latter being preferred).整数参数需要通过引用传递(指针或引用,后者是首选)。 Right now you pass in a copy of the born_year and married_year values.现在,您传入了born_yearmarried_year值的副本 When you get the input you save that into local variables;当您获得输入时,将其保存到局部变量中; the passed parameters are never updated.传递的参数永远不会更新。

You want something like this:你想要这样的东西:

void getdata(char* name, int &born_year,int &married_year)

With that tiny change (adding the two & characters) your code should work as you expect.有了这个微小的变化(添加两个&字符),您的代码应该可以按预期工作。

If you want passed variables to be changed inside a function you need to pass them by reference.如果您希望在函数内更改传递的变量,则需要通过引用传递它们。 To do that you need to make some changes in your function definition:为此,您需要对函数定义进行一些更改:

void getdata(char* &name, int &born_year, int &married_year) {
   //things you function does
}
#include <iostream>
#include <string>

using namespace std;

class person {
public:
    char name[19];
    int born_year, married_year;
};

void getdata(char* name, int &born_year, int &married_year) {
    cout << "Enter Name!\n > ";
    cin.getline(name, 19);
    cout << "What's the Born Year?\n > ";
    cin >> born_year;
    cout << "What's the Married Year?\n > ";
    cin >> married_year;
}

void print(char* name, int born_year, int married_year) {
    cout << "Your Name : " << name;
    cout << "\n Your Born Year : " << born_year;
    cout << "\n Your Married Year : " << married_year;
}


int main(void) {
    person p;
    getdata(p.name, p.born_year, p.married_year);
    print(p.name, p.born_year, p.married_year);
    return 0;
}

As others have mentioned your problem was that you were passing copies of the values in, not the variables themselves.正如其他人提到的,您的问题是您传递的是值的副本,而不是变量本身。 Also you forgot to put the "p" on "married_year" in your main loop.您还忘记在主循环中将“p”放在“married_year”上。

However, rather than passing in the separate values by reference, a much nicer idea is passing in a reference to the whole person:然而,不是通过引用传递单独的值,一个更好的想法是传递对整个人的引用:

int main(void) {
    person p;
    getdata(p);
    print(p);
    return 0;
}

Getdata and print would look like this: Getdata 和 print 将如下所示:

void getdata(person &p) {
    cout << "Enter Name!\n > ";
    cin.getline(p.name, 19);                          
    cout << "What's the Born Year?\n > ";
    cin >> p.born_year;
    cout << "What's the Married Year?\n > ";
    cin >> p.married_year;
}

void print(const person &p) {
    cout << "Your Name : " << *(p.name);
    cout << "\n Your Born Year : " << p.born_year;
    cout << "\n Your Married Year : " << p.married_year;
}

It's a cleaner way to do it, and since you're not creating or passing as many variables around, there are less ways to make a mistake here.这是一种更简洁的方法,而且由于您没有创建或传递尽可能多的变量,因此在这里犯错的方法更少。 If you made the fields private however, you'd need to make the two functions "friend" of the class to access its members.但是,如果将字段设为私有,则需要使类的两个函数成为“朋友”以访问其成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM