Okay I am writing a database type of program, which will store data about people.

I want to create a class:

class Person {
public:
    int age;
    int weight;
    int height;
};

Okay now I want to do something like this:

cout << " Enter name of person.\n";
cin >> name;
//create an object for the class "Parent" and name the object using the user inputed name
Person ("name");  

//add information to that object 
cout << " Enter age.\n"; 
cin >> age;
age.("name") = age;

cout << " Enter height.\n";
cin >> height;
height.("name") = height;

cout << "Enter weight.\n";
cin >> weight;
weight.("name") = weight;

But that doesn't work.

Oh and the purpose of this is so that I can do something like this:

cout << "Enter name of person you want to view info about.\n";
cin >> person;
cout << age.("person") << height.("person") << weight.("person");

I having been googling for the past half hour and can't find any answer.
So how do I create an object with a name that is inputed by the user?

Also I wrote the same program using arrays(when my C++ book talked about arrays) now I am learning about classes and I am rewriting it. So when it comes to performance...is it better to store data into objects that are part of a class or store them in arrays and use indexing? Also which will require more sys resources? Right now the only advantages I see to doing it this way than using arrays is that there is a limit to how much arrays can store and the program is much easier to write.

Any help would be greatly appreciated.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Please, read your C++ book again (at least how to declare class variables and access its members).
It seems all you know about C++ at the moment: // comments and operator << ;)
Probably you can't understand the answer to your question now ( age.("name") = age; - it's cool! )...

create this class...

class Age
{
    //...up to u..:)
};

class Height
{
    //...up to u..:)
};

class Weight
{
     //...up to u..:)
};

class Person
{
     //...up to u..:)
};

Please, read your C++ book again (at least how to declare class variables and access its members).
It seems all you know about C++ at the moment: // comments and operator << ;)
Probably you can't understand the answer to your question now ( age.("name") = age; - it's cool! )...

Uhm... no. I condensed everything so that I can target my main question.

And cikara..yea I know I can create seperate classes but is there way I can create a universal class that I can create objects out of.
And the object's name has to be something from the user.

The constructor of your class simply needs to be called after the values have been input, and passed those input values.

class Person
{
string Name;
int Age;
Person(const string name, const int age) : Name(name), Age(age) {}
};

That syntax is called an "initialization list". It means class variables are assigned during the construction of the class.

int main()
{
//input from user
string name;
int age;
cin >> name >> age; //or however you want to input them
Person(name, age);
}

That should construct the class with the user input values.

Hope that helps.

Dave

The constructor of your class simply needs to be called after the values have been input, and passed those input values.

class Person
{
string Name;
int Age;
Person(const string name, const int age) : Name(name), Age(age) {}
};

That syntax is called an "initialization list". It means class variables are assigned during the construction of the class.

int main()
{
//input from user
string name;
int age;
cin >> name >> age; //or however you want to input them
Person(name, age);
}

That should construct the class with the user input values.

Hope that helps.

Dave

Thanks a lot Dave, I really appreciate it.

--Solved--

Sorry its not solved.
This still doesnt solve my problem.
I think I phrased my question incorrectly.
How do I give a user the ability to name the objects that I create?
For example if they enter "Bob"
I want to create an object with "Bob:

class example{
};

example "Bob"

Sorry its not solved.
This still doesnt solve my problem.
I think I phrased my question incorrectly.
How do I give a user the ability to name the objects that I create?
For example if they enter "Bob"
I want to create an object with "Bob:

class example{
};

example "Bob"

So you want a new variable with the variable name Bob , which is of type example ? If it's possible, I definitely don't know how to do that. I was thinking along the lines of what daviddoria was suggesting, which apparently isn't what you want.

For example if they enter "Bob"
I want to create an object with "Bob:

daviddoria's idea does that. "Bob" would be stored as the Name data member of his Person class object. But it would be stored as the data, not a variable/object name.

Seems like you are possibly going for an associative array, which would be a map in C++.

http://www.yolinux.com/TUTORIALS/CppStlMultiMap.html

Comments
I think the map is what he wants, and if even if he can't use it, you were correct to point it out.

Vernon no I dont think you see what I am asking.
Let me try again.

Okay I have a class.
To create an object in the class you do the following:

Classname "objectname";

The object name could be like obj1, obj2.
I was asking if there was any way to allow the user to create the object name.

For example:

class Vehicle{
...
};

cout << "You have chosen to add a car to inventory."
cout << "Enter new car name.\n";
cin >> carname; 
Vehicle "carname"; 
//create an object with the name USER ENTERED
// for example if they entered honda i want to create an object called honda

If that helps explain what I am saying?
Sorry there isnt like an easy way to ask this.
The link you gave me is very useful thanks :) but I can't use it for this purpose.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I honestly don't think that is possible, but there are others here who are far more knowledgeable on these issues than I am, so don't take my word for it. But I am doubtful that this is possible. Hopefully someone else will see this thread and be able to give you a better explanation and perhaps (if I am wrong and there IS a way to do it) a solution.

To the best of my knowledge, it is not possilbe, and I can't imagine a scenario where it is really required.

I'm pretty sure that the map example (associative array) is really what he wants to do.

He wants to create an object and associate it with the name the user typed so that he can find the object again using the same name (possibly the user typed it again) later.

There is no reason that the code should have to have the variable name match. This is one of those places where the software needs to present an interface to the user that is not quite how the software implemented it. It is not all that uncommon of an occurance when working near the user inputs. It should not be a requirement that the user conform to how the program thinks about or deals with the data, it is up to the program to take the data from the user in the most convient way for them and massage it as necessary to get it to 'fit' the programs way of thinking.

As long as the software can accept the name the user enters to 'name' the object so that later it can be retrieved (and/or updated) through that name (whether remembered or re-entered), I believe that it has met the user request.

Newto, if you think that an implementation that makes it appear to the user as if you named the variable the name they typed in is not sufficient, feel free to convince me otherwise.

Is there more to the problem than being able to retrieve the data when they type the name again?

Murtan thanks for the information.
But there is more to it.
I want the object created upon user entry.
Say I wanted to create a database with, I don't know, say 100,000 entries.

It would just be so simple to ask for a name.
Create objects with that name.
Using the name to enter data into that object.
View data using name.

I am really new to C++ but later on I think I could load the objects into a .config file and call upon the objects based on user entry.

The only thing I would have to do is implement error checking on name, something like this:

bool errorcheck( string user_entered_name)
{
    bool idcondition;
    string name_error_check;

    name_error_check = object("user_entered_name").age 

// id is free is age is less < 0 because age has to be positive
    if(name_error_check < 0)  
         {
              idcondition = true;
          } 

    if(name_error_check > 0) // there is something  in that object 
         {
              idcondition = false;
              cout << "Name taken.\n";
         }
}

Thanks for the help.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

But there is more to it.
I want the object created upon user entry.
Say I wanted to create a database with, I don't know, say 100,000 entries.

It would just be so simple to ask for a name.
Create objects with that name.
Using the name to enter data into that object.
View data using name.

All this can be done and has been done many times using a map. I've never seen it done the way you want to and again, I don't think it's even possible. As Murtan mentions, are you sure that you need to do it the way you are trying to do it and that a map won't do the job well?

Ok this will be a bit of code, but bear with me. The concepts came from the tutorial link from the earlier post and some test code I wrote to make sure I understood how it works. (I've never used a map before today, but I've used similar objects from other languages.)

#include <map>
#include <string>
#include <utility>

void testcode()
{
    struct Person
    {
        int age;
    };

    map<string, Person> mapOfPerson;
    // or if you prefer you can use a typedef
    typedef map<string, Person> PersonMap;
    PersonMap People;

    // To add someone to the collection, you can do:
    People["Jim"].age = 19;

    // behind the scenes, when you referenced "Jim" the map found that he didn't exist
    // and then added him with a default value

    // This just adds Carl, but doesn't set his age
    People["Carl"];

    // You could also add someone "the hard way"
    Person sample;
    sample.age = 22;
    People["Bob"] = sample;

    // Once someone has been added, you can do things like:
    cout << "Bob is " << People["Bob"].age << endl;
    // Bob is 22

    // To determine who is in the list (or to find if someone exists) requires an iterator
    PersonMap::iterator finder;

    finder = People.find("Carl");
    // ok this part is a little more involved...but you can do things like:
    cout << finder->first << " is " << finder->second.age << endl;
    // Carl is 0

    // The previous example presumed that we would find what we searched for
    
    // We can find someone who doesn't exist,
    finder = People.find("Sally");
    // But if we tried to print what we found, it generates an assertion at runtime
    // cout << finder->first << " is " << finder->second.age << endl;
    // If someone is not found, finder points to the end of the list
    // So this code is safe, even though we didn't find Sally
    if (finder != People.end())
        cout << finder->first << " is " << finder->second.age << endl;
    // (Nothing prints because the if test failed)

    // To see all of the people in the collection:
    for (finder = People.begin(); finder != People.end(); ++finder)
    {
        cout << finder->first << " is " << finder->second.age << endl;
    }
    // Bob is 22
    // Carl is 0
    // Jim is 19

    // The collection is ordered by the 'key' value (in this case, the name)

    // to do something like your validity check:
    bool idcondition;
    string user_entered_name = "Bob";	// just to define it in this scope
    PersonMap::iterator ff = People.find(user_entered_name);
    if (ff == People.end())
    {
        idcondition = true;
    } 
    else 
    {
        idcondition = false;
        cout << "The name " << ff->first << " is taken." << endl;
    }
    // The name Bob is taken.

}
Comments
Exactly what I was looking for

Exactly what I was looking for.
Thanks so much Murtan.
I know someone else mentioned using map but your code helped understand what they meant.
Thanks again.

~Solved~

This question has already been answered. Start a new discussion instead.