mercredi 6 avril 2016

c++: How to pass a raw string to a function like evaluate( "2+2\0+2" )?

EDIT:: std::string infix="2+2\0+2"; how to pass it to the function evaluate(std::string s){cout << s}? => 2+2+2 ?


I am writing a simple calculator in c++11 with tests, supporting an infix notation.

And I have a function int evaluate( const std::string & infix ) which calculates the result from my string (i.e. evaluate( "((-3+--4*-(-19+5)))" ) returns 53). And everything works just fine until...

When I create some tests I can write ASSERT_EQ(6, evaluate("2+2+2")). But when I write ASSERT_EQ(6, evaluate("2+2\0+2) everything goes wrong (this should throw an exception but it returns 4).

So how to pass an array of characters into a function, not knowing it size and having null characters inside of it? When const char cstr[] = "2+/0+2"; evaluate(cstr); does not send a full array :(

Aucun commentaire:

Enregistrer un commentaire