Keo
Keo

Reputation: 5

The attributes of a user made object won't print (C++)

I am trying to make an object with a series of user inputs but it doesn't seem to be working.

Here is the code: (No Errors occur)

Making the class and using a constructor function so if it wasn't user made it would be easier to enter details:

#include <iostream>
using namespace std;

class Person{
    public:
    string FirstName;
    string Surname;
    string Gender;
    int Age;
    double Money;
    Person(string aFirstName, string aSurname, string aGender, int aAge, double aMoney){
        aFirstName = FirstName;
        aSurname = Surname;
        aGender = Gender;
        aAge = Age;
        aMoney = Money;
        }
                
    };

Declaring the variables that will be substituted in later and getting them with a user input:

int main(){
        string bFirstName;
        string bSurname;
        string bGender;
        int bAge;
        double bMoney;
        cout << "What is your character's First Name?" <<endl;
        getline(cin, bFirstName);
        cout << "What is your character's Surname?" <<endl;
        getline(cin, bSurname);
        cout << "What is your character's Gender?" <<endl;
        getline(cin, bGender);
        cout << "What is your character's Age?" <<endl;
        cin >> bAge;
        cout << "How much money does your character have?" <<endl;
        cin >> bMoney;

        // Then substituting the variables into an object:

        Person Custom1(bFirstName, bSurname, bGender, bAge, bMoney);

        // And finally trying to print the gender out 
        // (I have tried all the other as well with no success) 
        // and ending the program:

        cout << Custom1.Gender;
        return 0;
}

Upvotes: 0

Views: 228

Answers (1)

Mike Smith
Mike Smith

Reputation: 238

Your constructor has the initializations the wrong way around. Try this:

Person(string aFirstName, string aSurname, string aGender, int aAge, double aMoney){
        FirstName = aFirstName;
        Surname = aSurname;
        Gender = aGender;
        Age = aAge;
        Money = aMoney;
        }

Upvotes: 1

Related Questions