Is directly putting in binary possible C++ [duplicate] - c++

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;
}

Related

Print Minimum & Maximum Value Of Data Types in C++ [duplicate]

This question already has answers here:
(C++) INT_MAX and INT_MIN could not be resolved?
(4 answers)
How come INT_MAX and INT_MIN resolve in C++ without <climits> [duplicate]
(1 answer)
Why it seems not necessary to include some STL headers
(2 answers)
C++ code runs with missing header, why?
(1 answer)
Closed 3 months ago.
I was watching a tutorial on YouTube and I seen her coding this to print min and max value of data type. You can see in screenshot below. Her code was working perfectly but mine is not.
Here's My code and It's giving error.
#include <iostream>
using namespace std;
int main(){
// Print Minimum & Maximum Value Of Data Types
cout << "Minimum Value Of Int Is " << INT_MAX << endl;
system("pause>0");
}
The macro INT_MAX is defined in the <climits> header file. You need to include it if you want to use the macro.
If the Youtube video doesn't say anything about that, then perhaps it's not that good source for learning, and you should find another way (and it's much too easy to find bad videos than good, so I recommend you stay away from Youtube).
For a more C++-ish way instead include <limits> and use the std::numeric_limits class template.
More specifically its max static member function:
std::cout << "Max int value is " << std::numeric_limits<int>::max() << '\n';

Using an int variable in system() function c++ [duplicate]

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.

Converting calculation contained in string to integer - C++ [duplicate]

This question already has answers here:
Evaluating arithmetic expressions from string in C++ [duplicate]
(7 answers)
Closed 1 year ago.
I was wondering if it would be possible to store caluclations contained in a string to integer. For example:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string variable = "-123+432";
cout << stoi(variable) << endl;
}
This returns -123, would it be possible to make it return 309?
Also what if there would be more calculations inside the string (eg. "-123+432*2"), and the person writing program would not know how many calculations will there be in a string (eg. string would be entered by user when the programm is runing).
Thanks for all answers
It's possible for sure, but it's quite complicated to parse arbitrary strings, work out if they contain a valid mathematical expression and then work out the result.
Unless you are wanting to implement the solution yourself for fun, I would suggest looking up and using a 3rd party library that evaluates string expressions, for example https://github.com/cparse/cparse
This would allow you to do something like (probably not exactly correct, just for rough example):
int main()
{
string variable = "-123+432";
std::cout << calculator::calculate(variable, &vars) << std::endl;
}
If you are wanting to do this yourself for fun, I suggest you look up "Expression evaluation" and start from there. It's quite a large and complicated topic but a lot of fun to write your own evaluator imo.

Passed array inconsistent bahevior; C++ [duplicate]

This question already has answers here:
Using sizeof on arrays passed as parameters [duplicate]
(3 answers)
What is array to pointer decay?
(11 answers)
Closed 5 years ago.
I'm trying to learn some basic C++ and the moment I started to think I got a grasp of all those pointers I stumbled across this problem:
int sizeOf(string texts[]) {
return sizeof(texts);
}
int main() {
string texts[] = {"apple", "banana", "orange", "watermelon"};
cout << sizeof(texts) << endl;
cout << sizeOf(texts) << endl;
}
and this function returns
128
8
My question is: what is happening when I pass this array as an argument? Why suddenly C++ forgets that this is an array? I have tried to dereference it inside a method (return sizeof(*texts)) but it returned 32 instead which is the size of one string element, not a whole array. Is it possible to do what I want to do? Where am I mistaken?

Please explain how this c++ program is working? [duplicate]

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.