C++ the value of a variable in a string [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to make a function that has one parameter (a string that contains the name of a variable in my program) and returns the value that the variable stores.

Unfortunately, C++ doesn't provide a simple mechanism for reflection (the idea that the program can, at runtime, look up variable names by value or the like). You may need to consider using an alternate approach.
For example, you could make a std::unordered_map whose keys represent certain values that you'd like to look up and whose values are populated with the items you'd like to look up.
Hope this helps!

Related

From String to Function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'd like to know if there is a way to transform a string into a function em c++. For example if I have the string: "x+y" it'd create the function and by replacing x and y, get the value of it.
In Java there is this API https://www.objecthunter.net/exp4j/index.html, so I was wondering if there is something similar.
There are many possible methods one could use to transform a string into a "function". Many of those involve parsing the string and building a function-like object out of it.
A lightweight and portable solution would be to use ExprTk, a mathematical expression library developed by Arash Partow.
The main page contains various usage examples.

Should I make two versions of one program, each with different language, or add an option to change language? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have a program that I'm currently writing in English. In future, I would like to make the program multilingual, therefore I'm wondering what's the best option to do so. I thought about this two option for now:
Enable users to change language in settings;
Select the appropriate language while downloading;
With each option comes a problem:
A ton of code dedicated to displaying one message in different languages;
I will have to make many versions just to change the language of the displayed text;
Now my question is, which one of these options is more memory efficient and user friendly? Maybe neither of them are? Do you have any other option that is better than given two?
It is common to have an array or other structure, called something like strings, containing all the display strings your program will need. Instead of hardcoding messages into your program, you reference the array. To change language, you just alter the array.

How do i use Qvarient in cpp for set functions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to pass values from qml to set function written in Qt cpp, the values that will be passed is of different data types, example int or string, I will be writing only one set function in cpp which will take this values and return Qstring or int or double.How can I write a code for this.
C++ does know of two types:
QVariant
QJSValue
which might be the value, that you passed... That depends on you.
Both of them have various methods to test for their content, and to convert to this.
See the linked documentations for this.
You may even store the passed value as the corresponding type, without the need of conversion (up to the time, you need it for calculations in C++) It depends on, what you will do with it, to find the right choice.

Style for naming a function that is setting an argument by reference? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
So pretend I'm developing a car class and I want one of the car class's functions to return a list of passengers, except I'd like to put a list reference as an argument and just set that list instead of returning a list.
void GetPassengerList(PassengerList &passengerList); //sets the list
I don't know if I should call it GetPassengerList or SetPassengerList, or something else. I feel like using the words get / set make it seem like there is a private variable that is being manipulated like the typical getter / setter methods. What's a good naming convention to use here?
In our team for input/output arguments we either use
void AdjustPassengerList(PassengerList&);
or
void AddPassengersTo(PassengerList&);
Depending on the use-case. For example the first one could be used if you want a list created from more than one car. The second usually reads well in code, something like:
car.AddPassengersTo(list);

Declaring certain elements as const in an array c++, sudoku solver [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible for me to take an existing array and make certain values (not all) constant? I'm trying to build a sudoku solver and my idea is to have the user enter values, so I would like to have those values remain constant as I change the empty spaces. Any tips or advice with the solver would also be appreciated. This is my first quarter working with c++. Thanks!
No. Const is essentially a compile-time idea. It's not something that can be toggled as a program is running.
If you need certain values to remain untouched while others are changed, then you need to put that into your data and logic. For instance, each value might have an associated boolean that indicates whether or not it can be changed. Then write your logic to respect that boolean.