How do I print out the info from a class??

Hello everyone, I am basically creating a class called Person which holds member variables for a person's first name, last name, age, weight, and height.


My question is how do I cin those variables in order to allow user input, then print whatever the user inputted?
Here is my code so far, I'm not sure how I would cin the variables for user input and then output what the user inputs basically:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>

class Person {
private:
    
    std:: string mfirstName;
    std:: string mlastName;
    std::string getName;
    int mAge;
    double mHeight;
    double mWeight;
    
public:
    void reset (std::string firstName) {
        
        std::string getName();
        mfirstName = "";
        mlastName = "";
        mAge = 0;
        mHeight = 0;
        mWeight = 0;
        
        
    }
    
    void print () {
        std::cout <<"Enter first name!" << std::endl;
        
        std::cout <<"Enter last name!" << std::endl;
        
        std:: cout <<"Enter age:" << std::endl;

        std:: cout <<"Enter height:" << std::endl;
    
        std:: cout <<"Enter weight:" << std::endl;
       
    }
};

int main () {
    
    Person obj;
    std::string firstName = "";
    
    std::cin >> firstName;
    std::cin.ignore();
    obj.reset(firstName);
    
    std::cout << "num in obj is " << obj.getName();

    
    
}
Last edited on
You could write an input taking function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void input_Person_data( 
        Person& person
      , std::istream& istr = std::cin
      , std::ostream& ostr = std::cout
) {
    std::string word;  // for string input
    int int_nr;      // for integer input
    double double_nr;  // for double input

    ostr << "Enter first name: ";
    std::getline( istr, word );
    person.setFirstName( word );
    ostr << "Your first name is " << word << ".\n";
    ...
    ostr << "Enter age: ";
    istr >> int_nr;
    person.setAge( int_nr );
    ostr << "Your age is " << int_nr << ".\n";
    ...
}

int main()
{
    Person person;
    input_Person_data( person );
    ...
}
Last edited on
Topic archived. No new replies allowed.