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.
Related
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';
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:
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 an answer here:
Array[n] vs Array[10] - Initializing array with variable vs numeric literal
(1 answer)
Closed 12 months ago.
What i would like to do is declare an array with "dim" size :int A[dim];.
Now, this works if I declare something like const int dim = 1 but doesn't with const int dim = round(x);, which is what i need to do. (Where x comes from cin >> x.)
Note: With "doesn't work" i refer to Visual Studio Code throwing red wavy line under dim in int A[dim]; and displaying the following when hovering it with my mouse:
`
expression must have a constant valueC/C++(28)
main.cpp(15, 11): the value of variable "dim" (declared at line 13) cannot be used as a constant
`
This is the relevant code:
#include <iostream>
using namespace std;
int main(){
float x;
cin >> x;
const int dim = round(x);
int A[dim];
int i = 0;
}
}
Given the context i believe the error is caused by one of two reasons:
Some characteristic of round() that makes the const int dim = round(x) not recognized as constant from the array later.
The problem is the x and not the round() so cin >> x is the reason.
[Thanks for whoever can explain me what I'm missing or point to some documentation that does. I have done some research but I haven't found a solution to this. Also this is my first question on SO, so tell me if I should change/improve something]
EDIT: Apparently the problem isn't in the round(x) as I previously thought because simply replacing const int dim = round(x); with const int dim = x; gives the same "error".
So the problem has to do with cin >> x .
EDIT 2 Note: I'm looking for a solution that doesn't use std::vector. We haven't studied it yet in the course so I believe the algorithm(from which i took the relevant code) shouldn't comprehend it.
Final Edit I didn't realize that, as #paulmckenzie clarified, using cin made the array dynamic because the imput comes in runtime, it was a really stupid error but I apologize, I'm really a beginner. In my defense we really haven't talked about dynamic size arrays so I guess that's what threw me off. I realized from the beginning I was missing something very basic, sorry for wasting time, I'll put even more time analyzing everything before posting next time.
The size of an array variable must be compile time constant in C++. User input is not compile time constant, hence it cannot be used as the size of an array variable.
In order to create an array with runtime size, you must instead create a dynamic array. The simplest way to do that is to use std::vector from the standard library.
EDIT 2 Note: I'm looking for a solution that doesn't use std::vector.
It's possible to create a dynamic array without std::vector, but that requires the use and understanding of more advanced concepts. Using new expressions directly is more difficult, error prone and is something that isn't (or shouldn't) be done in most programs in practice.
Of course, another solution is to just not use user input but rather an array with constant size.
This question already has answers here:
Define bitset size at initialization?
(7 answers)
Closed 5 years ago.
Now I've this code:-
The code is about taking in an integer and providing its binary form in the given number of bits.
#include <iostream>
#include <bitset>
using namespace std;
int main(){
//creating instance using bitset (6 bit). here you can specify the length such as 8,16,32,64...
int n=5;
bitset< 6 > btFlaged;
//assigning integer value to instance
btFlaged = 7;
//print bit string in the string
for(int i=btFlaged.size()-1;i>-1;i--)
{
cout <<btFlaged.test(i);
}
}
How do I use an integer(E.g. n) in the place of '6' so that a value entered at run-time can be used in the code?
I've done some research on the net and I know that bitset needs a value at compile time and so instead of bitset, I should use vector bool but I don't know how I should incorporate that in the program?
If any of you guys can tell me how to use vector or if you have an altogether different theory on how to the the task done, please do share.
Also I cant use boost:dynamic_bitset as the code is going to be judged by an online judge which may not have the seperate header file.
a std::bitset's size must be set at compile time as it is a template parameter. If you need a dynamic bitset you can look at boost:dynamic_bitset