Q: Basically I am reading unknown lines in length from a file in the form of a string
I than to split it into the next. Number1, operator, and Number2
The program reads in principle in things like the following: *
1234 4323
42,113,977,503,352 23,977,503,321 + p *, + and P are the only valid operators and the format is always number string, space, operator, space, number string.
I Im using unsigned = (); str.find the length of the first number string (i – 1 to find), unsigned str.find OP = (* + p) to the local operator to find. and unsigned k = str.find (/ n) to the length of the pipe to find.
I use something like: 977 503 977 503 for (i = num1_len-1, i = 0, i -) num1 [i] = (int) (str [i] – 0 ); 977 503 for (j = oplocal) OP = (char) (str [j]) for 977 503 (k = linelen, k = num1_len 3, k -) num2 [k] = int (str [k] – 0 );
to make strings in char arrays, but im not sure if this is really an appropriate method at all when I play in the strings.
Re: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.
Re: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.
Re:I still think the istringstream approach is best. You can start with it as a string and put it into the istringstream for parsing.
Re: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!
Re: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;
Re:Have you looked into the strtok c++ function? It may work the way you want.
http://cplusplus.com/ref/cstring/strtok.html
Re: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.
Re:is there a tokenizer in C? You can check for number or letters by nval and sval
Re:you should be able to use the substr() function to get a substring.
0 Comments.