I'm writing a c++ calculator but I keep getting stuck on the part which changes the std::string into float variable for mathematical calculation.
I've already tried atoi and using 'var' (single-quote) but it seems to result in erratically large numbers and some variations of the code won't even compile saying "Line 13 Column 18 C:\Users\User\Desktop\calculator.cpp [Error] cannot convert 'std::string {aka std::basic_string}' to 'float' in initialization".
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string>
#include <stdlib.h>
int main()
{
std::cout << "Input arithmetic calculation: \n";
std::string calc;
std::cin >> calc;
atoi( calc.c_str() );
float result=calc;
std::cout << "Result = ";
std::cout << result << '\n';
}
I expect the output to be calculated such as 10*9=90 but it ends up as 10*9 or (when adding single-quote to calc in float result=calc) 1.93708e+009.