String Conversion help

Hello, Relatively new to C++ and working on a program that parses data input from a file. I have some values stored in vector<string> arrays and I have to compare different columns of the input for various reasons. The problem I'm having:

I do a string compare on 2 strings that as far as I can see, are the same. I've checked the string size() and its teh same. I've tried to use substrings to make sure they are the same size and it still doesnt recoginze that they are the same.
Here is what I'm doing, code posted below.

Line of input is parsed into strings. Say one string is this:LISTA,ENDA
I use a string explode function(in the code below) that seperates the string into a vector array using the comma as a seperator.

I then scan part of a struct that also holds strings and compare them to see if they are the same. Looking at the strings, printing them to screen etc I see no difference in the strings but the .compare method does not recoginize they are the same string. I'm thinking there may be a problem with the stringexplode function but I cant figure out what. Any help would be appreciated. I've posted partial code because my entire code is to long. If you have any insights on the stringexplode function let me know. I'm not averse to using another method to explode the string if thats the problem.

One more thing, the first element of the array created by stringexplode is compared fine, it finds it match, its just the second element ENDA that is n ot being found which is why I think its teh explode function but I cant figure out what is wrong.

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
struct ext_sym_table{
    int ctl_sec;
    string ctl_sec_name;
    string symbol;
    int loc;
    int ctl_sec_length;
    char type;
}eSymTab;



    //create extsymtab for current file
    eSymTab.ctl_sec = ctl_secctr;
    eSymTab.ctl_sec_name = headerName;
    eSymTab.ctl_sec_length = locctr - (input[0].loc);
    eSymTab.loc = input[0].loc;
    estab.push_back(eSymTab);
    for(int i=0;i<extdefs.size();i++){
         string exdef = extdefs[i];
         cout<<"Exdef: "<<exdef<<endl;
            for(int j=0;j<input.size();j++){
                string lab = symtab[j].symbol;
                cout<<"Label: "<< lab<<endl;
                if(extdefs[i].compare(symtab[j].symbol)==0){
                        //.compare((symtab[j].symbol).substr(0,exdef.size()))==0){
                    cout<<"YESH"<<endl;
                }else{
                    cout<<"NOOOOO"<<endl;
                    cout<<"e: "<<exdef[0]<<" "<<exdef[1]<<" "<<exdef[2]<<" "<<exdef[3]<<" "<<exdef[4]<< exdef[5]<<endl;
                    cout<<"l: "<<lab[0]<<" "<<lab[1]<<" "<<lab[2]<<" "<<lab[3]<<" "<<lab[4]<< lab[5]<<endl;
                    cout<<"error: "<<exdef.compare(lab.substr(0,exdef.size()))<<endl;
                    char *one = new char[exdef.size()+1];
                    one[exdef.size()]=0;
                    memcpy(one,exdef.c_str(),exdef.size());
                    char *two = new char[lab.size()+1];
                    two[lab.size()]=0;
                    memcpy(two,lab.c_str(),lab.size());
                    cout<<"Error2 "<<strcmp(one,two)<<endl;
                }
            }
}
void StringExplode(string str, string separator, vector<string>* results){
    int found;
    found = str.find_first_of(separator);
    while( found!=string::npos){
        if(found>0){
            results->push_back(str.substr(0,found));
        }
        str = str.substr(found+1);
        found = str.find_first_of(separator);
    }
    if(str.length()>0){
        results->push_back(str);
    }
}

Input file
1
2
3
4
5
6
7
8
9
10
11
12

0000    PROGA    STARTX   0XXXXXXXXXXXXXXXXXXXXXA  0000000A
                 EXTDEF   LISTA,ENDA
                 EXTREF   LISTB,ENDB,LISTC,ENDC
0006    CLOOP   +JSUB     RDREC                    4B101036
0020    REF1     LDA      LISTA                    03201D
0023    REF2    +LDT      LISTB+4                  77100004
0040    LISTA    EQU      *
0054    ENDA     EQU      *
00A7    REF3     LDX     #ENDA-LISTA               050014
1036    RDREC    CLEAR    X                        B410
                 END      REF1
Last edited on
the problem of 'StringExplode()' is on line 50. the function 'find_first_of()' has two paramters. The second one is the offset. Currently you're always starting from 0 and hence you find always the same separator. It should be found = str.find_first_of(separator, found+1);
I tried your suggestion, what it did was create strings with the , . Like I said, the explode function creates substrings of the parsed line. But when I do a compare on it with another element that it should match, it doesnt. Say in a line like this

LISTA,ENDA

It parses LISTA into a string and it compares fine, also I checked its .size() and its 5. But when I try to compare ENDA it does not match a string that it should. Also I checked its size after explode and its 5 when there are 4 chars. Could stringexplode be adding a '\0' or something to the end I need to get rid of to compare it to a string? Im at a loss, I can not figure out why two strings that appear equal will not compare but its only the second element which is also the last item parsed by string expldoe that will not compare correctly.

Not sure if this helps but when I do
exdef.compare(lab.substr(0,exdef.size()))
where exdef is the string ENDA from the stringexplode function and lab is a string from a struct array the error numbers are
string = -1
cstring = -19
Ok so if I do a substr(0,4) on the ENDA string generated by the expldoe function it compares correctly with a string ENDA from a struct. Apparently there is some hidden character at the end of the string generated with string explode?

Any idea how I can get rid of it? The words parsed are of random size so I cant just substr(0,4) to make it work.
found an answer using a string trim function. Thanks!
Are you aware that substr() extracts a substring but doesn't remove it from the original string?

On line 52/53 you push_back() the whole string not the remainder. You must explicitly call erase() to remove the extracted string
Topic archived. No new replies allowed.