简体   繁体   中英

C++ having input to class

I made 2 function : getdata, print for input/print variable in person class

    #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

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

you can remove variables declared in main function and use p. before every parameter.

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

Your problem is in the variable types to your getdata function. 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. 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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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