954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Trivial stuff

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?

TrintiyNoe
Newbie Poster
24 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 
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?

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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

TrintiyNoe
Newbie Poster
24 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

@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 ???

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 
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.

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 

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

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

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

TrintiyNoe
Newbie Poster
24 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

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??

TrintiyNoe
Newbie Poster
24 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 
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 )

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

About namespaces, Tom Gun has written an excellent post. Read it http://www.daniweb.com/forums/post903119.html#post903119

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

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

TrintiyNoe
Newbie Poster
24 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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.

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

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 giving the order of magic box codes

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

TrintiyNoe
Newbie Poster
24 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You