Hey,i just came by an example where,a string was split off using strings, the input was something like, "342c234"

basically, a string where it is full of numbers and in between a char,
i used ascii values in a for loop to split them,but somebody just did some sort of cool thing strings,i dont know wat,but just it was two lines using istringstream,can anybody explain it to me?
or where i can read more abt it?

but somebody just did some sort of cool thing strings,i dont know wat,but just it was two lines using istringstream,can anybody explain it to me?

If the char is always a 'c' you could do something like:

string str  = "";
    stringstream sstr("342c234");
    while (getline(sstr, str, 'c')) cout << str << '\n';

Line 2 declares a stringstream and loads it with your input string.
We need this stringstream because we want to use getline to parse the stream and check for the delimiter character 'c'.
Line 3 calls getline with as argument 1 the created stream. Argument 2 is a buffer where we want to store the ouput given by getline. Argument 3 is the delimiter-character, in your example the 'c'.
For each value given back by getline, line 3 also outputs the string to the screen with a cout

No its not, it varies

we first get the string from user as an argument,then lets say it "213a234"

we seperate the string and add the two numbers and ,and display the char

Then I wouldn't use stringstreams to parse the string.
You could use something like:

int main()
{
    string input = "b123cc456duedj9a";
    string str = "";
    for (unsigned i = 0; i < input.length(); i++){ //loop through each element
        if (!isalpha(input[i]))  // is alphabetic ? 
            str += input[i]; // add it to the string
        else {
            if (str.length()) { // only if we already have some number
                cout << str << '\n'; 
                str = ""; // clear the string
            }
        }
    }
}

This takes a string of any length, with any number of character and numbers in it and only displays the numbers.

What you still need to do is:
- convert the (std::string) str to an int
- display the characters

No,the input wont be as random as that
It is in the format

(int)char(int)

123e3269 or 78c24
not 45df56(two char)
it contains only one char

and also converting is the one of the main part i want help with

No,the input wont be as random as that
It is in the format

(int)char(int)

123e3269 or 78c24
not 45df56(two char)
it contains only one char

Doesn't matter, the code will run fine.

and also converting is the one of the main part i want help with

Have you tried this?

Found the solution :)

#include<iostream>
#include<vector>
#include <sstream>
#include<algorithm>
#include<math.h>
using namespace std;

class SimpleCalculator
{
    public:  
	int calculate(string input)
	{
		int i,j;
		char op;
		stringstream w;
		w<<input;
		w>>i>>op>>j;
		return i+j ;
		
	}
};

Thanks for helping :) Especially niek_e

U rock

This article has been dead for over six months. Start a new discussion instead.