cin<<a;

lets say the user enters 'i'

if i have a variable i,i want its variable to be displayed
or some arbitary "Invalid output" message,
any way of doin this,instead of using n case statements in switch????

Is there a way for direct redirection?

cin<<a;

lets say the user enters 'i'

if i have a variable i,i want its variable to be displayed

So you mean:

int i = 1;
char a;
cin >> a; //user enters 'i'

and you want to display '1'? If so -> not possible. You could use arrays, vectors, maps are something to do something similar?

Yeah thats exactly what i wanted,sad to know not possible

Yes, it is not possible. But ask your self: you you really need it? I never felt need of such a thing. The only close alternative is to use a std::map something like this:

int main()
{
  using std::map;using std::string;using std::cout; using std::cin;
  map<string,int> m;
  m["i"]=1;
  m["k"]=5;
  m["ok"]=53;
  string input;
  cout<<"Enter a variable name";
  cin>>input;
  cout<<"You entered "<<m[input];
}

@siddhant3s : Well what you have written is for predefined things,that is if a programmer knows that he is going to initialize variables by name x y z then he can maintain an array to know the values stored in those variable names. And its static. The compiler does it every day every time. May be not possible in C C++ but a lexer and parser combination can do it I suppose.And may be it can even be done in Assembly.Isn't it guys ???

Yeah thats exactly what i wanted,sad to know not possible

What you want is called reflection. C++ has a very limited type of reflection in the RTTI stuff, but it doesn't work for variable names because variable names are lost during compilation. You have to map input strings to objects like siddhant3s showed, or some other awkward workaround.

The real problem is that end users should not need to understand the workings of your code to use the program. Instead of typing 'i' and that name being a direct link to a variable in the program, I should be able to use a more abstract user variable if you want me to have that kind of control:

#include <iostream>
#include <map>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    map<string, int> userVar;
    string field;

    cout << "User variables ({name:value} {name:value} ...): ";

    while (getline(cin, field, ' '))
    {
        istringstream iss(field);
        string name;
        int value;

        if (iss.get() == '{' &&
            getline(iss, name, ':') &&
            iss >> value)
        {
            userVar[name] = value;
        }
    }

    cout << "Current user variables\n";

    map<string, int>::const_iterator 
        begin = userVar.begin(),
        end = userVar.end();

    for (; begin != end; ++begin)
    {
        cout << begin->first << ':' << begin->second << '\n';
    }
}

It's kind of like siddhant3s' code except the end user picks the names instead of the programmer. The programmer doesn't care what names are used.

This is actually what the debugger does more properly ... right???

What langage is reflection possible? atleast without a lot of difficulty?

>What langage is reflection possible? atleast without a lot of difficulty?
Python and Lisp as far as I know.

Yes, it is not possible. But ask your self: you you really need it? I never felt need of such a thing. The only close alternative is to use a std::map something like this:

int main()
{
  using std::map;using std::string;using std::cout; using std::cin;  //this
  map<string,int> m;
  m["i"]=1;
  m["k"]=5;
  m["ok"]=53;
  string input;
  cout<<"Enter a variable name";
  cin>>input;
  cout<<"You entered "<<m[input];
}

Sorry for being a troll,but instead of

using std::map;using std::string;using std::cout; using std::cin;

cant i just use "using namespace std"??
or is it recommended to use it like the one above??

What langage is reflection possible? atleast without a lot of difficulty?

Python for example (as already mentioned by siddhant3s).
But the real question is: Why would you want to do that? There's probably a better way to achieve your goal then switching to another language.

How about you tell us what you want from your program an maybe we can suggest some alternatives. ( as siddhant3s and Tom Gunn already did actually )

Well its like a puzzle, there are boxes , named like aa,ab,ac,ad,ae,ba, ....... ee

and values in those are like bc,ab, .... randomly shuffled

smallest or largest steps one should have to take for a given order

I know it would be better to do it without reflection,but just curious :)

Can u ppl suggest method for too,i am kinda stuck there

Well its like a puzzle, there are boxes , named like aa,ab,ac,ad,ae,ba, ....... ee

and values in those are like bc,ab, .... randomly shuffled

smallest or largest steps one should have to take for a given order

I know it would be better to do it without reflection,but just curious :)

Can u ppl suggest method for too,i am kinda stuck there

Be precise and specific in your definition of your problem and post the part which is giving you errors.In that way we can help you better.

We have some Magical Boxes,each with an code before its entrance and inside it

The codes are of the form,"AA,AB,AC,AD,AE,BA,BB.......,EE;

The codes in the entrance of the boxes are ordered
The codes of magic boxes are jumbled like "BA,ED,....."
All the codes are present in the boxes,everyone of them is unique
If a person enters a box,the person would end up in the entrance which corresponds to the code;

For example,if he enters a box AA,he'll go the entrance AA, after the entrance if the box has a code EA,he'll go to box EA.
Kinda like redirection,over and over again,till he finds the entrance to the box he first came in through AA.

If the input is given to us is a vector<string> giving the order of magic box codes

Our job is to find the maximum and minimum number of steps or redirections he can take.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.