How to refer to a variable ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to refer to a variable ?

Hi am trying to write a code like this Int i = 3 Pi I want the Pi to turn into P3 in this case , how can I do that ? Thanks in advance

9th May 2018, 10:21 AM
Dyary
Dyary - avatar
3 Answers
+ 2
Dyary If what is printing out P3 you can do this: cout << "P" << i << endln; But if what you want is use P3 in an expression like: P3 + 2 I don't know if that is possible. First because in C++ you need to declare a variable before using it second, I believe there is no eval function in C++, althought, I'm not sure. An eval function would receive a string and evaluate it like an expresion: eval('P3 + 2'); But I believe only dinamic programming languages have such function.
9th May 2018, 10:41 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 2
May I ask why you'd like to do that? If it's about using several Pi's, better use an array P
9th May 2018, 2:00 PM
Matthias
Matthias - avatar
+ 2
Yea it for several Pi’s. I used an ary it works that way. Thanks
9th May 2018, 2:02 PM
Dyary
Dyary - avatar