This question already has answers here:
Simple C++ - about strings and concatenation and converting int to string [duplicate]
(3 answers)
Closed 6 years ago.
This may seem trivial to normal C++ users, but I'm currently relearning C++ all over again. I have a function that takes in an int& and I would like to add that value to a string for a print statement. So, I have something like this:
std::string getTest(void* dataGroup, int& xWidth, int& yHeight)
{
std::string results = "";
results += "\nxWidth is: " + xWidth;
return results;
}
which fails to run when I call it. What is the proper way to convert the pass-by-reference int& value into a form that can be added to a string?
std::to_string(xWidth).
The & is pretty much irrelevant. to_string takes the int by value, so how your function takes it doesn't matter: by value, lvalue ref, rvalue ref, const or non-const... all will work.
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:
Convert string to variable name or variable type
(7 answers)
Closed 5 years ago.
If the title is not so suggestive I hope the explanation might help you understand my problem.
I have a function which sets some variables value. But the variables name I would like to provide it as a string, this way:
void setValue( const std:: string& variable_name, int value){ variable_name=value;}
then when I call this function like this:
setValue("variable", 10);
I will expect to execute set variable=10;
Any idea if this approach is possible or other ways I could have this behaviour?
Thanks!
It is not possible to magically retrieve a variable from a run-time string. You need to provide a mapping in advance, which is probably not what you want. E.g.
int& getFromName(const std::string& s)
{
if(s == "variable") return variable;
if(s == "foo") return foo;
// ...
}
This question already has answers here:
Why does call-by-value example not modify input parameter?
(6 answers)
Closed 8 years ago.
int Test = 11;
void Toggle(const char* Name, int Offset, int Value){
Offset ^= Value;
printf("%s\n [%s^7]", Name, Offset ? "^2ON" : "^1OFF");
}
In main...
void main(){
Toggle("Unlimited Ammo", Test, 100);
Toggle("Unlimited Ammo", Test, 100);
}
I'm pretty sure XOR is suppose to toggle this. I don't know why its not working for me.
C++, by default, is pass by value, meaning the function gets a local copy of Test. Changes to it are not reflected back to the caller.
If you want pass by reference, you have to use ... wait for it ... references :-)
You do that with a function signature like:
void Toggle(const char* Name, int &Offset, int Value) ...
This question already has answers here:
c++ change function's variable argument
(2 answers)
Closed 8 years ago.
Say I have
string stringInput = "hello";
alter(stringInput);
cout << stringInput;
and a function:
void alter(string stringIn){
stringIn[0] = stringIn[3];
}
Ideally I would want cout to produce "lello". But right now it simply returns "hello" as originally. I know this has something to do with addresses and pointers... how would I achieve this?
It's actually just because a new copy of the string is created for use in the function. To modify the string directly in the function add an & before the string name in the function header like this:
void alter(string &stringIn){
That passes the string by reference. Otherwise you could just return a string from the function.
All you need to do is to pass the string by reference:
void alter(string& stringIn){
// ^
stringIn[0] = stringIn[3];
}
You should also modify accordingly any function declarations you have for alter().
Your stringIn is a local variable. So when you pass it on the function as a value it just makes a new stringIn with different address. So the changes you are making in alter is only affecting the new stringIn. You need to recieve the reference of the stringIn in alter to make it work.
void alter(string& stringIn){
stringIn[0] = stringIn[3];
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is address of char data not displayed?
I was experimenting with ampersand operator and got stuck at this program :
#include<iostream>
using namespace std;
int main() {
char i='a';
cout<<&i;
return 1;
}
I was expecting the address of variable i as the output but instead the output came as the value of variable i itself.
Can anybody explain what just happened? Thanx in advance.
That's because cout::operator<< has an overload for const char*. You'll need an explicit cast to print the address:
cout<<static_cast<void*>(&i);
This will call the overload with void* as parameter, which is the one used to print addresses.
Also note that your code runs into undefined behavior. You only have a single char there, and the overload expects a null-terminated C-string.