First, you need to initialize nr. Right now it's a junk value.
Second, you are trying to put an element into an index of input that is out of bounds.
I believe you just mean cin >> input;
Third, if input[b] is 0 ('\0'), you should stop iterating because that marks the end of the string you just entered.
Now... boilerplate aside, the actual logic needs to be discussed. You are multiplying nr by its previous value each time. What should the initial value of nr be? If it's 0, you're multiplying by 0 each time.
Perhaps you meant nr = nr * 10 + (input[b] - '0');
But, this just gets you back 123, as an integer. It certainly doesn't produce 4196691. Where does that number come from? Can you explain it in words?
and I'm stuck at this point, I can't put a char into an int.
I use Qt IDE and qmake to build this project.
Excuse me if I made big grammar mistakes, I have a bad English.
#include <iostream>
#include <string>
#include <sstream>
int eval( const std::string& str )
{
// put the string into a string stream (we want to ead integers and operators from it)
std::istringstream stm(str) ;
int left = 0 ;
if( stm >> left ) // read the first number (lhs)
{
char op ;
int right ;
while( stm >> op >> right ) // in a loop, read the operation, next number (rhs)
{
// note: error handling elided for brevity
// noisy stuff to trace what is going on. may be commented out
std::cout << left << ' ' << op << ' ' << right << " == " ;
// evaluate, make the first number equal to the result
if( op == '+' ) left += right ;
elseif( op == '-' ) left -= right ;
elseif( op == '*' ) left *= right ;
elseif( op == '/' && right != 0 ) left /= right ;
// noisy stuff to trace what is going on. may be commented out
std::cout << left << '\n' ;
}
}
return left ; // nothing more to be read, return the result
}
int main()
{
const std::string str = "1+7-3*60/6+4*12+18/11-28" ;
std::cout << "evaluate " << str << "\n\n" ;
constint result = eval(str) ;
std::cout << "\nresult == " << result << '\n' ;
}