How to assign a string value to integer variable in c++? [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 have a very basic question how can I assign a string value to a variable that already has integer value. For example, int a = 10 then I need to assign a ='hey'. Is it possible in c++?

I think you are new to C++? In C++ you assign the variable a fixed type (int in your case). It is not possibile to change that type inside the scope. You have to convert a to a string and save them in a new variable. How to convert a string (char* or std::string) you can look here:
Easiest way to convert int to string in C++

Related

Same behaviour for size() vs .size() with string? [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 1 year ago.
Improve this question
Is there any difference in output behaviour between using size(x) instead of x.size(), where x is a string variable? Or it is just another alias?
The behavior of std::size is:
Returns the size of the given container c or array array.
1-2) Returns c.size(), converted to the return type if necessary.
...
So calling std::size(x) and x.size() has the same effect, where x is a std::string.
(This function can also accept an array, but that's not relevant when it comes to a std::string).

Convert const char* to std::string without constructors [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 4 years ago.
Improve this question
I always wondered why one can't just convert null-terminated C string into std::string(without constructors, writing helper functions and other workarounds)? It appears to be a simple and overly commonplace problem, where you have, let's say argv[1] null-terminated string as an argument passed to your program(or any other naturally occurred C string) and you need to pass it's value to a function directly.
I.e
How do I wrap the cstr in-place without allocating new string object?
This is the answer as best I can tell.
const char * cstr = "Hello World.";
...
..myfunc(cstr);

Why is const char INITIAL='G' not an assignment statement? [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 6 years ago.
Improve this question
I am having problems in understanding this statement. I don't know why this is not usual like others.
Assignment means giving a new value to an already existing object. Even though const char INITIAL='G'; has an = sign, it is not an assignment, because it is creating a new object, not modifying an existing one. char INITIAL; INITIAL='G'; would be an assignment, because INITIAL already exists when the new value is, well, assigned.

c++ binary value for indexed array [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 6 years ago.
Improve this question
I am extremely new to C++ so I'm probably asking a very trivial question, but if you could help that'd be great!
I have an array[n].
Indexed from 0 to some unknown value.
I need to access the index of the array, the n value but I need to do so in binary. I am intending to do a bit reversal on it.
So, if I have an array of 2048 points how do I represent the 1024 array in binary?
If you want to write a value in binary, you can do so in C++14 with
int my_binary_value = 0b01010101;
If you'd like to test a specific bit of an int, you can do that by masking it, i.e.
bool is_bit_4_set = my_binary_value & 0b00001000;

How to get variables to my program vrom text file [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 8 years ago.
Improve this question
I have a file like:
a [able%5:00:00:capable:00] [able] capable
a [abnormal%3:00:00::] [abnormal]
a [absent%3:00:00::] [absent]
a [absolute%3:00:00::] [absolute] perfect or complete
a [abstract%3:00:00::] [abstract] existing only in the mind
a [abundant%3:00:00::] [abundant] plentiful
I want to get first column "avle", "abnormal" etc to my object. How to crop them and how storage them?
Use the string tokenizer, the best/easiest way to split a line into different variables.
char* word = strtok(line," [%:]");
char* word2 = strtok(0," [%:]");
int value = strtoi(strtok(0," [%:]"));
to store there is a vector container, but there can be used any type of arrays, what is more convenient