What works:
double a1;
a1 = 2+4;
std::cout << a1;
What I want it to be:
double a1;
std::cin >> a1;
std::cout << a1;
But when I type 2+4, I get the output 2, then 4, I want it to sum at once.
What works:
double a1;
a1 = 2+4;
std::cout << a1;
What I want it to be:
double a1;
std::cin >> a1;
std::cout << a1;
But when I type 2+4, I get the output 2, then 4, I want it to sum at once.
C++ will not magically do the math for you, you need to parse input, check for errors and do the calculation. For simple math you can use stream operations, to make it more advances I suggest regexp, for further more advances syntax you need parser http://www.boost.org/doc/libs/1_59_0/libs/spirit/repository/example/qi/calc1_sr.cpp.
std::regex pattern("(\\d+)\\s*(\\+)\\s*(\\d+)");
std::string line;
while (std::getline(std::cin, line)) {
std::smatch sm;
if (std::regex_match(line, sm, pattern)) {
int val1 = std::stoi(sm[1]);
std::string op = sm[2];
int val2 = std::stoi(sm[3]);
std::cout << (val1 + val2) << "\n";
}
}