help taking a string and splitting it into char arrays in C++

Jul 20, 2005
72
0
0
basically i am reading lines in of unknown length from a file in the form of a string

I then have to split it up into the following. Number1, Operator, and Number2

The program basically reads in things like the following:

1234 * 23
321 + 42113
352 p 4323

*, +, and p are the only valid operators and the format is always number string, space, operator, space, number string.

I am using unsigned i = str.find(' '); to find the length of the first number string(i - 1), unsigned op = str.find('+*p'); to find the local of the operator. and unsigned k = str.find('/n'); to find the line length.

I then use something like the following:

for(i=num1_len-1 , i=0, i--) Num1= (int)(str-'0');
for(j=oplocal) op= (char)(str[j]);
for(k=linelen, k=num1_len+3, k--) Num2[k]= int(str[k]-'0');

to make the strings into char arrays but im not sure if this is really a correct method at all as I am just getting into strings.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
you should be able to use the substr() function to get a substring.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
Actually, overall, what you're looking for is a split function like you find in many other languages/libraries. The standard C++ library's string doesn't have a split function, but it's not hard to write one or find one on the web. I found this bit of code which looks right on the surface (but I haven't tried various test cases on it myself). It takes in a string and a vector. In your case, the vector would end up with three elements for the components around the spaces.

 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
why not use istringstream?
http://www.fredosaurus.com/notes-cpp/strings/stringstream-example.html
istringstream instream; // Declare an input string stream
instream.str(s); // Use s as source of input.

// OP do this:

int num1, num2;
char op;

instream >> num1 >> op >> num2;


istrinstream works just like cin but gets it's chars from the string you give it, cin gets them from stdin. If you can use cin forget istringstream and use it directly instead of getting your input line by line as a long string. Or was the whole point of the exercise to do string manipulations?

C++ input streams (by default) ignores all whitespace and 'knows' how to handle basic typesby default.
this line:
instream >> num1 >> op >> num2;
will first, discard any whitespace characters it sees, then read a sequence of digits and convert it to it's correpsonding numeric value ad store it in num1
then it will read the next char (because op is a char) into op.
then it will read the next sequence of numbers into num2

you don't have to stringstream at all, you can get your input straight into the 3 components with cin, like so:

cin >> num1 >> op >> num2;
 
Jul 20, 2005
72
0
0
Thanks for the help everyone! Statik: Unfortunately this is an excercise in strings so we are required to read it in as a string otherwise we dont get credit. I am working on using substr right now and if that doesnt work the way I's like i may try the strtok. Also with the instream, there is a limit to the number of digits I can havem which is why I need to go from string to character array as this is how my multiply and add classes opperate. I wish I could use C# as there are much more efficient ways of doing it b/c the libraries have allready been written, but alas, this is C++...
I'll be getting more help tomorrow but for now this definately helped get the ball rolling, thanks everyone!
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
I still think the istringstream approach is best. You can start with it as a string and put it into the istringstream for parsing.
 
Jul 20, 2005
72
0
0
unfortuinately I am dealing with numbers with 50+ digits for some of these Num1 & 2's. I need it to go from a string to a char array, not an integer.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
then i like my Split function better. it will give you a vector of three strings. from a string, you can get a char array, if that's what you really need.