0

hi, I am trying to make a calculator and I need the stringstream function to convert a a certain part of a string.

string n1 = "243+79";
int n2 = 0;
stringstream(n1[0-2]) >> n2; // this converts the 243 from n1 to an int

Any solutions?

Edited by thecoolman5: n/a

4
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by thecoolman5
0
std::string asd="234+34";
   char plus;
   int first,second;
   std::stringstream (asd) >> first >> plus >> second;
0

Also, i need a function that can remove parts of a string.

That's what the substr() function does.

string n1 = "243+79";
    string first = n1.substr(0,3);
    string second = n1.substr(4,2);

In other words string.substr(starting_index, number_of_characters);

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.