-
December 24th, 2008, 08:16 PM
#1
C++ class objects created based on user input
I am relatively new to C++ and I can't seem to find the answer to this question anywhere on the net.
Okay I have a class.
To create an object associated with that class you do the following:
Classname "objectame";
I was wondering if there was any way to allow the user to create the object name.
I would need something like that for a program like this:
class Vehicle{
int mpg;
int topspeed;
...etc
};
cout << "You have chosen to add a car to inventory.";
cout << "Car name: ";
cin >> carname;
Vehicle "carname"; //create an object with the car name that was specified by the user
Then when the user needs to view data I want to be able to let them type the name and let the view stuff:
cout << "Enter car name.";
cin >> carname;
cout << "carname".topspeed << "carname."mpg"....
So how do I go about accomplishing this?
Any help is appreciated.
-
December 24th, 2008, 09:11 PM
#2
Re: C++ class objects created based on user input
No there isn't. You could create objects and store them in a map that maps a string to the object it you want to refer to them by some use entered name.
-
December 24th, 2008, 09:14 PM
#3
Re: C++ class objects created based on user input
Thats not how it works. What BOOK are you using to learn C++. (Informal / Random approaches do not statistically work).
Code:
class Vehicle
{
public:
Vehicle(std::string name) : Name(name)
std::string Name;
};
std::map<std::string, Vehicle> Cars;
cout << "You have chosen to add a car to inventory.";
cout << "Car name: ";
std::string carename;
cin >> carname;
Vehicle myCar(carname);
Cars[carName] = myCar;
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 25th, 2008, 02:20 AM
#4
Re: C++ class objects created based on user input
to get data on a car based on user input of the car's name, you might want to make a vector of cars and compare the user input to the names of the stored cars and output the desired members of the car when a match is found
Code:
std::vector<Vehicle> cars;
...
std::string searchName;
cin>>searchName;
for(int i=0;i<cars.size();i++)
{
if(searchName==cars[i].Name)
{cout<<cars[i]; //overloaded operator<<}
}
I think that is what you wanted to do anyway
-
December 25th, 2008, 02:41 AM
#5
Re: C++ class objects created based on user input
Originally Posted by drfool
to get data on a car based on user input of the car's name, you might want to make a vector of cars and compare the user input to the names of the stored cars and output the desired members of the car when a match is found
To expand on GCDEF's suggestion and TheCPUWizard's example, I would suggest something like this instead:
Code:
// ...
std::string searchName;
std::cin >> searchName;
std::map<std::string, Vehicle>::iterator found = Cars.find(searchName);
if (found != Cars.end())
{
std::cout << found->second; // overloaded operator<<
}
-
December 25th, 2008, 09:53 AM
#6
Re: C++ class objects created based on user input
Laserlight and doctorfool yea thats the way I want to implement viewing the info, thanks
TheCPUWizard thanks I think thats exactlywhat I have been looking for. But do you have a specific tutorial on map? I have been using http://msdn.microsoft.com/en-us/beginner/cc305129.aspx
Maybe i just have to finish of the book because I just finished learning about classes.
I am going to use the map method that you guys gave but I am just curious.
Before I submitted this thread the way I created it(temporary solution) was:
class Vehicle
{
string name
...
};
int main()
{
Vehicle object[100]; //create an array of objects in the vehicle class
int id;
string entered_name;
cout << "Enter id for new vehicle.";
cin >> id;
cout << "Enter name";
cin >> entered_name;
// then added stuff to it
object[id]. name = entered_name;
...
//viewing id is same thing
cout << "Enter id to view vehicle.";
cin >> id;
cout << object[id].name << object[id]...... // and then showed everything else
}
well it works but how do I prevent the array from automatically taking up memory.
what I mean is when the program starts 100 objects are created. I don't want an object created unless that array is called for. Anyone have an idea how to do that? So this way vs the map method that TheCPUWizard posted? Which would be better?
One more thing...
Sorry but you guys seem really helpful
when I created the array method, I didnt want users to remember the id when viewing the data.
So I created something like this, which doesnt work(any help?):
string entered_name;
string name_in_data;
const char *entered_name_pointer;
const char *name_in_data_pointer;
int for_loop;
const max_storage = 100; //max_storage is the maximum # of users that the database program can store
cout << "Enter name.\n";
cin >> entered_name; // get entered name
entered_name_pointer=entered_name.c_str(); //convert the string to a char
for(k=0;k<max_storage;k++) //max_storage is the maximum # of users that the database program can store
{
name_in_data =object[k].stored_name; //retrieve the name in data for that id(k) and then pass it to name_in_data
name_in_data_pointer =name_in_data.c_str(); //convert that to a string...(i dont want to attempt conversion on (object[k].stored_name])
do{
userid = k; // the correct user id is k if...(refer to while)
break;
}while(strcmp(name_in_data_pointer, entered_name_pointer)== 0); // if the strcmp() proves that there is a name like that in the data
} //end of for
//now the right user id is recieved from the for loop above
cout << object[userid].name;//show car name
cout << object[userid].....//etc
The thing is my entire program uses the string datatype(string.h)
But I need strcmp() but strcmp() only compares char strings.
I don't want to change my entire program to using chars just for this.
So above is my pitiful attempt to convert the name entered by user(for viewing data) and all
the name_stored in data to char then get it compared. And then display the data once i get the userid.
Again, this vs laserlight and doctorfool's method with maps to view the data which will work better?
Last edited by Swish44; December 25th, 2008 at 10:35 AM.
-
December 25th, 2008, 11:35 AM
#7
Re: C++ class objects created based on user input
First, use code tags when posting code. The code you posted is not formatted and is hard to read.
Second, instead of online tutorials, you should be using proper C++ books. The std::map is discussed in any C++ book. It is also documented in thousands of places on the Internet, so asking one of us to show you std::map isn't fair, when usage of map is shown in so many places.
Third, instead of arrays, use std::vector. This is probably the class you're looking for. If not, there is std:eque and std::list, all well documented.
Fourth:
Code:
string entered_name;
string name_in_data;
const char *entered_name_pointer;
const char *name_in_data_pointer;
Stop using char* and pointers for strings!. There is absolutely no need for it. If you studied by using proper books, char* and pointers shouldn't even have been mentioned to you. Always use std::string for string manipulation in C++ (this is not 'C'), unless you have a compelling reason to use char*.
Code:
entered_name_pointer=entered_name.c_str(); //convert the string to a char
Why do you need to convert this to a pointer?
There is no need at all to be using char*, strcmp() or any of those 'C' methods.
Regards,
Paul McKenzie
-
December 25th, 2008, 11:41 AM
#8
Re: C++ class objects created based on user input
To compare std::string, you use the <=, >=, !=, ==, etc, the same normal operators you use for other simple data types. You don't use strcmp() or any of that.
Pretend that strcmp(), strcpy(), and all of those functions do not exist. Anytime a simple C++ program uses strcmp() and char*, IMO, it isn't coded correctly. The only exception is if the program is written to show how difficult and error-prone it is to use these types of constructs (instead of using the very simple to use std::string).
Regards,
Paul McKenzie
-
December 25th, 2008, 11:54 AM
#9
Re: C++ class objects created based on user input
The thing is my entire program uses the string datatype(string.h)
string.h should NEVER be included in any C++ program. Period.End of Discussion. ABSOLUTELY no reason to EVER do it.
Is the proper header for C++ strings.
If you MUST use C-Style strings. First write a multi-page document explaining WHY, get it approved by a recognized exprert, then ask them what the proper header is... IT IS NOT string.h!!!!!
(21.4 Null-terminated sequence utilities of the ISO standard is where you find the answer)
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 25th, 2008, 12:04 PM
#10
Re: C++ class objects created based on user input
Paul sorry about the inconvenience.
I will make sure I finish my book before posting further questions.
Thanks for you second post, didn't know you could use operators like >,<, = with strings. This opens up a new world for me
Also, yea I forgot to use bb code.
TheCPUWizard, I didn't know there was a difference between the .h and just <>. Thanks for pointing that out.
So, is my understanding correct:
Code:
//in c
#include "windows.h"
Code:
//in C++
#include <windows>
Last edited by Swish44; December 25th, 2008 at 12:08 PM.
-
December 25th, 2008, 12:15 PM
#11
Re: C++ class objects created based on user input
Originally Posted by Swish44
TheCPUWizard, I didn't know there was a difference between the .h and just <>. Thanks for pointing that out.
The standard C++ headers do NOT have a .H. This should be in the very first chapter of your book!!!!!
So, is my understanding correct:
Code:
//in c
#include "windows.h"
Code:
//in C++
#include <windows>
NO. "Windows.h" is NOT a standard C++ header.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 25th, 2008, 12:23 PM
#12
Re: C++ class objects created based on user input
Originally Posted by Swish44
So, is my understanding correct:
No. The first example typically causes the compiler to search for a file named "windows.h" relative to the current directory. If such a file cannot be found, it would then be processed as if it were #include <windows.h>.
The second example typically causes the compiler to search for a file named "windows" relative to the compiler's own include directory and include paths as specified by the compiler's configuration.
Now, the C standard header for strings is <string.h>, but the C++ "equivalent" is <cstring>. The normal C++ standard header for strings is <string>. As TheCPUWizard noted, the C++ standard headers do not have a ".h".
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|