jeudi 30 août 2018

Vector of struct in c++, Not reading proper value

I have a loop which reads value and inserts them into a std::vector of structs.

Here's my code:

vector<PTNvalues> PTNdata;
PTNvalues pt;

struct PTNvalues
{
    string LowRangesValue_[15];
    //string LLowRangesValue_[15];
    string HighRangesValue_[15];
    string Service[15];
    int version;
    char key;
    string Desc;
    int Ranges;
    bool AllowAlphas;
};

for (size_t i = 0; i < 35; i++)
{
    //PTNdata.push_back(PTNvalues());
    if (i < 10)
    {
        FD = '0' + i;
        //FD = to_string(i);
    }
    else
    {
        FD = char(55 + i);
    }
    pt.key = FD;
    tempstr = MessageApp::App.Ini.ReadString(to_string(FD), "AllowAlphas", "N");
    ToUppercase(tempstr);
    if (tempstr == "T" || tempstr == "Y")
    {
        pt.AllowAlphas = true;
    }
    else
    {
        pt.AllowAlphas = false;
    }
    pt.Ranges = MessageApp::App.Ini.ReadInt(to_string(FD), "Ranges", 0);
    for (size_t t = 1; t <= pt.Ranges; t++)
    {


        pt.LowRangesValue_[t] = MessageApp::App.Ini.ReadString(to_string(FD), "LowRanges__" + to_string(t), "");
        pt.HighRangesValue_[t] = MessageApp::App.Ini.ReadString(to_string(FD), "HighRanges_" + to_string(t), "");
        pt.Service[t] = MessageApp::App.Ini.ReadString(to_string(FD), "Service____" + to_string(t), "");
        pt.Desc = MessageApp::App.Ini.ReadString(to_string(FD), "Desc_______" + to_string(t), "");

    }
    PTNdata.push_back(pt);

}

But when I read value from that vector I am not getting proper value.

Here's how I read values:

size_t ParseScanData2::keyToIdx(char k)
{
    if (k >= '0' and k <= '9') return k - '0';
    if (k >= 'A' and k <= 'Z') return k - 'A' + 10;
    throw runtime_error("invalid key");
}

string AA = PTNdata[keyToIdx('H')].LowRangesValue_[10];
string BB = PTNdata[keyToIdx('H')].HighRangesValue_[10];

Above my file has only 3 values for Low and High for 'H'. But I am getting different values for string AA and BB above which should be empty. Am I missing something?

Aucun commentaire:

Enregistrer un commentaire