Using Variables as substitutions for members for classes

This is a discussion on Using Variables as substitutions for members for classes within the C++ Programming forums, part of the General Programming Boards category; Kind of new at this, but I had a question that I couldnt answer on my own. I was fooling ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 10:07 PM
  4. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 08:01 AM
  5. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21