Thread: using user input as a var.

  1. #1
    nubi
    Guest

    using user input as a var.

    Hey i was wondering if there was anyway to take a 'string' and use it in place of an int/char or whatever.

    ie
    Code:
    cout<<"Type in what kind of shape you would like.\n";
    cin>>Shape_Name;
    
    Draw(Shape_Name);
    Im not sure if that makes sense...but lets say ive got a large program with many 'shape instances' and I dont want to make a huge 'switch/case'. So instead of 'switch/case' I just take user input and somehow change it into a 'variable' and pass it into a function.

    *shrugs*maybe that is a foolish request, but if that were possible...man that would open up a whole new world to me =/

  2. #2
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    if you are looking for a way to take in a string, one way is like this:
    Code:
    char myString[25];
    and replace the 25 with the maximum amount of characters it will hold. if you are afraid they will enter more digets than that, replace cin >> with this:
    Code:
    cin.getline(myString, 25);
    which will stop reading in characters after the 25 one, or whatever number you specify. im not sure if you can replace the switch/case with anything else though
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  3. #3
    nubi
    Guest
    wha???

    Thanks for the try, but i dont think you understood my question

    I understand how strings work(well at least fairly well)

    I was just wondering if there was any way to change 'User Input' into the 'name of a variable'.

    ie
    Code:
    int Add(string num_nam,int add);
    
    int main()
    {
        int num1 = 5;
        int add;
        string number_name;
    
        cout<<"Which number would you like to add to?\n"
               <<"And by how much?\n\n";
        cin>>number_name>>add;
        //user types in "num1" and 3
    
        cout<<Add(number_name,add);
        //output should be 8
    
        return 0;
    }
    
    int Add(string num_nam,int add)
    { 
        (typechange)num_name += add;
     
        return num_name;
    }
    does that make more sense?

  4. #4

    Nope. not possible in C/C++

    I think you are trying to emulate what many BASH shell scripers do:
    Code:
    #!/bin/bash
    read input_name
    read input_value
    $input_name="$input_value"
    export $input_name
    would allow you to change your TERM settings by:
    running it,
    typing "TERM" <enter>, and
    typing "screen" <enter>.

    There is no way to do such things in C/C++, because of the ways that C/C++ processes it and AS IT COMPILES IT. BASH and PERL (i think you can do it in PERL too) are (mostly) interpreted, which makes it easier to do this. The only way that I could see doing this would be something along the lines of using user unput as one of the fields of a template class:
    Code:
    ...
    char input[1024];
    cin >> input;
    newTemplate = template<graphic, input>;
    ...
    void polymorph(template<graphic, "SQUARE">) { ... }
    void polymorph(template<graphic, "CIRCLE">) { ... }
    ...
    but i'm wntirely lost when it comes to templates, this may not even be possible.
    Last edited by Inquirer; 05-03-2003 at 12:59 PM.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  5. #5
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    ohhh ok, now i understand what you are asking. unfortunatly, i dont know how to help you. ill look around, and if i find anything, ill post. i could have sworn i saw something on the subject a while ago, but it seems to have disappeared. maybe that was another site.
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  6. #6
    Nubi
    Guest
    hmmm templates...yea im not to profcient with those yet, but that could possibly be an option.

    Ill look into it thanks.

  7. #7
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Opinion

    I think that you can not use templates like that. Templates are not dynamic polimorphic. They are "configured" at compiling time. Uh! No good telling how things works.
    Anyway. You could use polimorphism(I donīt know how to write it correct) like this.
    Code:
    class Base {
       public:   
          virtual draw() = 0; //pure function, canīt be instantiated
    };
    
    class Circle : public Base{
       public:
          virtual draw(); //define this function somewhere
    };
    
    class Rectangle : public Base{
       public:
          virtual draw(); //define this function somewhere
    };
    
    int main(){
    
       Base* vect[2];
       vect[0] = new Circle();
       vect[1] = new Rectangle();
       
       cout << "Insert:\n";
       cout << "1.Circle\n";
       cout << "2.Rectangle" << endl;
       int option;
       cin >> option;
       vect[option]->draw(); //it draws the correct form
    }
    Hope that helps, but my english is too bad to give a good explanation.
    Last edited by gustavosserra; 05-04-2003 at 10:06 PM.
    Nothing more to tell about me...
    Happy day =)

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If you have to convert strings to commands or other strings or object pointers or whatever I recommend a std::map container to map them.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    nubi
    Guest
    std::map? I dont believe ive ever used this before, but ill search around and see how it works.

    I can see all sorts of things I could do with this, but Im not really sure that it is possible, considering that the program has already compiled the program, but dammit i think it would be neat

  10. #10
    cgoat
    Guest
    Here's a sample using std::map to convert a string into an int variable. The only limitation is that you have to hardcode all of the possible names to an existing variable.

    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    using std::map;
    
    int NumOne = 1;
    int NumTwo = 2;
    
    typedef map<string, int*> VarMap;
    VarMap theVarMap;
    
    int main(int argc, char *argv[])
    {
       // Set up map
       theVarMap.insert(VarMap::value_type("NumOne", &NumOne));
       theVarMap.insert(VarMap::value_type("NumTwo", &NumTwo));
    
       string sName;
       cout << "Enter Variable Name: ";
       cin >> sName;
    
       if(theVarMap.count(sName) > 0)
       {
          cout << sName << " has value of " << *(theVarMap[sName]) << endl;
       }
       else
       {
          cout << "No such variable!" << endl;
       }
    
       return 0;
    }

  11. #11
    nubi
    Guest
    Wow, that is great and amazing all at once. Thanks!


    *bows*

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Why map to int*, int is simpler.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    cgoat
    Guest
    Originally posted by CornedBee
    Why map to int*, int is simpler.
    The point is to map a string name to an actual variable. Mapping to int maps the string to a specific value.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    ok
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  5. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM