First, I think your Calculate()
method should take as input a const char*
string, since expr
should be an input (read-only) parameter:
double Calculate(const char* expr);
Note that if you use const char*
, you can simply call std::string::c_str()
without any ugly cast to remove const-ness.
And, since this is C++ and not C, using std::string
would be nice:
double Calculate(const std::string& expr);
On the particular issue of reading also whitespaces, this is not a problem of terminating NUL
byte: a space is not a NUL
.
You should just change the way you read the string, using std::getline()
instead of simple std::cin >>
overload:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string line;
getline(cin, line);
cout << "'" << line << "'" << endl;
}
If you compile and run this code, and enter something like Hello World
, you get the whole string as output (including the space separating the two words).