Search code examples
c++user-input

is there any way to let the user choose which variable is to be inputted?


this is for a personal use application in c++ for example:

class x                
{...};
int main()
{

string userinput;

cin>>userinput;

cin>>x."userinput";}

is something like this possible?


Solution

  • If what you are talking about is allowing for the user to set variable names at runtime, then no. It is not possible in c++. Depending on what you are trying to achieve, there are a few ways around this.

    If you just need the user to be able to access a small number of variables, all of which are known before hand, you can use a switch statement, or a bunch of if/elseifs

    If you need to store a larger amount of variables the number of which may not be known at compile time, then there exists a number of data structures that exist just for this purpose. There are arrays, hash tables, linked lists and hundreds of variations on the above. These are all far too complex topics to cover in a single answer however.