C
CarlWSummers
I'm doing something wrong. I'm parsing a string into rational numbers
(int/int) by looking for spaces in the string, assinging a substring of
the original string to a temporary variable, and trimming that
substring off the original string, perhaps this is more clear in code.
In any event, what I have works fine for strings like "23 1 23452 34"
and even "1/2 2/3 3/4 4/5" but if the string looks like "1/2 -2/3 3/4
-4/5" the original string becomes corrupted after the trimming.
I'm using VC 2003, but I'm rather sure this is my fault, and not the
compiler's.
#include <string>
using namespace std;
int main()
{
string line = "1/2 -2/3 3/4 -4/5";
string temp;
int index = line.find(' ',index);
temp = line.substr(0,index);
unsigned int size = temp.size();
string debug = line.substr(size);
line.assign(debug);
//line = debug;
}
After either line.assign(debug) or line = debug, line becomes
"ji¼è[Aã" or "Z".
If I put a watch on line it appears that there's a _Bx object with two
variables _Buf and _Ptr. _Ptr contains (...or points?) to the right
string, but _Buf contains garbage (different than above). I thought
that using an = with an object type resulted in a direct copy of the
object (versus, say, Java where the left side is just made to point to
the same object), but surely the assign function would make a copy even
if the = didn't.
The steps are a bit expanded, and I've been messing with it for a while
so it's not terribly clean. In any event, I'm a bit new to C++ and I
don't see what it is I'm doing wrong.
Thank you for your time,
Carl Summers
(int/int) by looking for spaces in the string, assinging a substring of
the original string to a temporary variable, and trimming that
substring off the original string, perhaps this is more clear in code.
In any event, what I have works fine for strings like "23 1 23452 34"
and even "1/2 2/3 3/4 4/5" but if the string looks like "1/2 -2/3 3/4
-4/5" the original string becomes corrupted after the trimming.
I'm using VC 2003, but I'm rather sure this is my fault, and not the
compiler's.
#include <string>
using namespace std;
int main()
{
string line = "1/2 -2/3 3/4 -4/5";
string temp;
int index = line.find(' ',index);
temp = line.substr(0,index);
unsigned int size = temp.size();
string debug = line.substr(size);
line.assign(debug);
//line = debug;
}
After either line.assign(debug) or line = debug, line becomes
"ji¼è[Aã" or "Z".
If I put a watch on line it appears that there's a _Bx object with two
variables _Buf and _Ptr. _Ptr contains (...or points?) to the right
string, but _Buf contains garbage (different than above). I thought
that using an = with an object type resulted in a direct copy of the
object (versus, say, Java where the left side is just made to point to
the same object), but surely the assign function would make a copy even
if the = didn't.
The steps are a bit expanded, and I've been messing with it for a while
so it's not terribly clean. In any event, I'm a bit new to C++ and I
don't see what it is I'm doing wrong.
Thank you for your time,
Carl Summers