Hey guys, i'm Working On A Project That Gets All Employees And Their Data From the User Then Saves Them in A Txt File ( Fstream )

Anyway, i figured out how to Do That But What im at stuck at now is how to search through the txt file , i figured that i got to get all the data from the txt file into the console when i start the program so i did all of that and divided all the data between < >
For example : < ID > <Name > <Rank>... etc
so When he Finds the < : He starts to Get the line Inside then when he finds the > He gets out of the If Condition

Anyway i got a Char C And defined it to be the First Array of the StructureArray so It GETS The "<" inside it but when i did that
i got this message " SubString Out OF Range" Can Anyone Plz Tell me what that means or Even how to fix it?

Here's a part of the Code

int index;
    string id;
    employees_data data[1000];
    char ch;
    vector <employees_data> MyData;
    fstream PRUJECT;
    PRUJECT.open ("Project.txt");
    employees_data tmp[1000];
    //
    string s[1000];
    string Name[100], address[100], rank[100], hoursworked[100], overtimehours[100];
char c;

    for(int i=0; !PRUJECT.eof(); i++)
    {

        // ID


            getline(PRUJECT,s[i]);
            index = s[i].find('=')+1;
            tmp[i].ID= s[i].substr(index, s[i].size()-1);
            MyData.push_back(tmp[i]);

            c=tmp[0].ID[0];

            if (c=='<')
        {   
            tmp[i].ID=s[i].substr(1, s[i].size()-2);
        }
        if (c=='>')
            continue;


        // NAME

            getline(PRUJECT,s[i]);
            index = s[i].find('=')+1;
            tmp[i].name = s[i].substr(index, s[i].size()-1);
            MyData.push_back(tmp[i]);

            c=tmp[0].name[0];
            if (c=='<')
        {   
            tmp[i].name=s[i].substr(1, s[i].size()-2);
        }
        if (c=='>')
            continue;


            And So On For The Rest Of The Data...

            AnyHelp is So Much Appreciated! Thanks : P

I'm just briefly looking at your code, but is there any case where the size of s[i] would be less than 2? if so, you'd then be subtracting 2 and passing a negative value to substr():

tmp[i].ID=s[i].substr(1, s[i].size()-2);

furthermore, your starting position is '1', so is there any case where the size of s[i] would be less than 1? (For example, trying to start at element [1] on a string of consisting of a single character, in which case you would start and end at element [0])

Edited 4 Years Ago by Clinton Portis

I'm just briefly looking at your code, but is there any case where the size of s[i] would be less than 2? if so, you'd then be subtracting 2 and passing a negative value to substr():

tmp[i].ID=s[i].substr(1, s[i].size()-2);

furthermore, your starting position is '1', so is there any case where the size of s[i] would be less than 1? (For example, trying to start at element [1] on a string of consisting of a single character, in which case you would start and end at element [0])

Well, Im trying to get the data from the txt file to the console without the < > , Just whats inside so i thought i would start from the first position to "-2' as its before the last ' > ' but i dunno it doesnt seem to work

for example, if the 'id' criteria is a single character, you will be passing in two erroneous arguments to the substr() function:

//if the size of s[i] is 1 char
tmp[i].ID=s[i].substr(1, s[i].size()-2);

the first argument will resolve to a starting index equal to 1, which is already out of bounds of the s[0] array. additionally, you will be subracting 2 from the array size, which in this case is 1; therefore, passing in a negative value to substr() which will result in an out of bounds error being thrown.

there are easy ways to obtain the implementation ye' desire, try it on your own and see if you can figure it out. come back with what you come up with and we can help you out.

Edited 4 Years Ago by Clinton Portis

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