Thread: Using Variables as substitutions for members for classes

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Using Variables as substitutions for members for classes

    Kind of new at this, but I had a question that I couldnt answer on my own. I was fooling around with classes, and I made several members of classes and I wanted to call them up based on what someone typed.

    Code:
        
    
    string mdl;
    cin >> mdl;
    
    cout << mdl.make << endl << mdl.year << endl << "$" << mdl.price << endl << "Space #" << mdl.space << endl << endl;
    This of course, doesnt work, because it thinks that mdl is some sort of member of the class and its saying that it doesnt have a make attribute etc etc. Im wondering how I can use mdl as a variable in this situation, so depending on what i typed in for mdl, It would give me all of its stuff. So if mdl = camaro, or mdl = escape, then it would give me camaro.make , or escape.make etc. in this situation.

    If its not possible like this, thats fine, I was just wondering.

    Thanks,
    Empire

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Look up std::map
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Thanks! This is what I'm looking for, im reading up on it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 09:01 AM
  5. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM