954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ stringstream problem

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?

thecoolman5
Posting Whiz in Training
234 posts since Dec 2010
Reputation Points: 18
Solved Threads: 1
 

Have a look at the substr() function

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

You can also think about strtol as a way to parse known good input.

L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 
Have a look at the substr() function


Could you please explain how to use substr()? thanks

thecoolman5
Posting Whiz in Training
234 posts since Dec 2010
Reputation Points: 18
Solved Threads: 1
 

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

thecoolman5
Posting Whiz in Training
234 posts since Dec 2010
Reputation Points: 18
Solved Threads: 1
 
std::string asd="234+34";
   char plus;
   int first,second;
   std::stringstream (asd) >> first >> plus >> second;
caut_baia
Posting Whiz
387 posts since Apr 2010
Reputation Points: 25
Solved Threads: 49
 
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);

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Ok thanks for the help.

thecoolman5
Posting Whiz in Training
234 posts since Dec 2010
Reputation Points: 18
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: