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! )...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
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
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
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
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
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.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
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
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
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?
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
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.
}
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116