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.
Related
This question already has answers here:
Can I use a binary literal in C or C++?
(24 answers)
Closed 9 months ago.
Is putting in binary as a value possible? I want something like char test = 00101011 and it will become 43. I know this is possible by making a function that converts binary to decimal (which can be inputted) but thats not direct and Im pretty sure it takes time.
You need to put the prefix 0b.
#include <iostream>
int main()
{
char c = 0b00101011;
std::cout << static_cast<int>(c) << std::endl;
}
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;
This question already has answers here:
"" + something in C++
(3 answers)
Closed 5 years ago.
I am having trouble , in understanding this program please help:
#include <iostream>
using namespace std;
int main(){
const char* s = 5+"hellow world";
cout<<s;
return 0;
}
It is correct and gives following output
In third line of your code, an anonymous character array is created by the compiler. When you add 5 to the c-string, it performs pointer arithmetic and moves the pointer 5 ahead to the string. Hence, it skips the 5 character from the c-string and only stores other characters from the array into the s.
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.
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.