This question already has answers here:
How to adress variable by name stored in another variable (C++)
(2 answers)
Closed 5 years ago.
std::string VariableName = "name";
int (VariableNameHere) = 5;
From my understanding of c++ what I am asking is most likely impossible. If it is please post possible alternative solutions. Thanks!
As you have it is not possible, you would need to have some kind of look-up system, such as:
std::map<std::string, int> variables;
...
variables["name"] = 5;
Related
This question already has answers here:
Easiest way to convert int to string in C++
(30 answers)
Closed 12 months ago.
I need using an int veriable in system() function for my c++ program.
for example:
int a = 0;
system("echo "a" ");
but i get an error and i need help about how i use this like that
Error:
C++ user-defined literal operator not found
That's never going to work. C++ doesn't plug integers into strings that way. Instead, you can do:
int a = 42;
std::string s = "echo " + std::to_string (a);
system (s.c_str ());
Also, you might consult this page, in order to learn the language properly.
This question already has answers here:
What does "new int(100)" do?
(4 answers)
Closed 1 year ago.
I came across two similar statements but could not exactly find the diference between them. The statements are:
int *p = new int(75);
int *p = new int[75];
Can anyone help me in knowing the difference between the above two statements.
The first returns a pointer to the integer with value of 75, but the second returns a pointer to the array of 75 integers, which don't have a value.
This question already has answers here:
converting a variable name to a string in C++
(8 answers)
Closed 2 years ago.
I'm looking for a function that returns a variable name
Theoretical example
template <typename Type>
std::string GetVarName(Type Var)
{
//Get Name
return Variable_name;
}
You could try to stringify the variable using the preprocessor. In the posted link by Schultke there are some examples using macro in order to achieve that.
This question already has answers here:
Array size at run time without dynamic allocation is allowed? [duplicate]
(8 answers)
Closed 4 years ago.
Total c++ newbie here.
I have a problem, where I can not assign the number of character in a string to an array size, like so..😓
string outStr;
ifstream input("read.txt");
getline(input, outStr);
int const n = outStr.length();
int arr[n];
error msg --> expression must have a constant value. although i have declared the "const"
Thanks in advance✌.
C++ does not support Variable Length Arrays. Use a std::vector instead.
This question already has answers here:
In what cases do I use malloc and/or new?
(20 answers)
Dynamically allocated C array of strings
(6 answers)
Closed 5 years ago.
I tried this
string *codes = (string*)malloc(256*sizeof(string));
codes[0] = "";
in C++. But it didn't work, but when I tried
string *codes = new string[256];
codes[0] = "";
This worked.
I did not understand the basic idea behind why this is happening. Could someone please tell me.
Thanks
This is because new uses constructor of the given class (in your case: std::string) and malloc() doesn't do this.