0

Good morning Stack Overflow,

I am having trouble getting some key-value pairs to store in a vector only if they meet certain criteria which I will explain below. Everything is working in my program so far except this.

To begin here is my main:

int main()
{
    Records passports;
    std::string filename = "test.txt";
    std::ifstream inFile(filename);
    std::string liNe;
    while (!inFile.eof()) {
        getline(inFile, liNe);
        if (liNe.length() == 0) passports.keyHolder2.push_back(" ");
        std::istringstream ss(liNe);
        while (ss >> liNe) {

            passports.keyHolder2.push_back(liNe);
           
        }
    }
    inFile.close();
    cout << isinRangeTest(passports) << endl;
}

Here is my structure to store all my vectors:

struct Records {

    std::vector<string> keyHolder;
    std::vector<string> validKeys;
    std::vector<string> keyHolder2;
    std::vector<string> validKeys2;

};

Here is my function to determine if they have the prefixes like "ecl, byr or iyr" and others:

int validPasstest(Records passports)
{
    //cout << passports.keyHolder2.size() << endl;
    std::vector<string>::iterator count;
    string x, z;
    int more = 0;
    bool plus = false;
    for (count = passports.keyHolder2.begin(); count != passports.keyHolder2.end(); count++) {
        //cout << *count << endl;
    }
    for (int i = 0; i < passports.keyHolder2.size(); i++) {
        z = passports.keyHolder2.at(i);
        std::size_t found = z.find("byr");
        if (found != std::string::npos) more++;
        found = z.find("iyr");
        if (found != std::string::npos) more++;
        found = z.find("hgt");
        if (found != std::string::npos) more++;
        found = z.find("hcl");
        if (found != std::string::npos) more++;
        found = z.find("ecl");
        if (found != std::string::npos) passports.validKeys2.push_back(z);
        found = z.find("pid");
        if (found != std::string::npos) more++;
        found = z.find("cid");
        if (found != std::string::npos) more++;
        found = z.find(" ");
        if (found != std::string::npos) 
        {
           /*if (more == 7) {
               plus++;
           }*/
           more = 0;
        }

    }

   
    return plus;
}

Here is my function to display the vector that only has valid key-value pairs:

int isinRangeTest(Records passports)
{
    for (int i = 0; i < passports.validKeys2.size(); i++) 
    {
        cout << passports.validKeys2.at(i);
   
    }
    
    return 0;
}

When I try to output validKeys 2 I get a "0" on the output window and nothing else. Here is my textfile and output:

ecl:gry pid:860033327 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm

iyr:2013 ecl:amb cid:350 pid:028048884
hcl:#cfa07d byr:1929

hcl:#ae17e1 iyr:2013 cid:150
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm

hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in cid:230


output

In my function validPasstest, I am trying to add 'z' to the vector validKeys2. It is not working. Any suggestions?

krainey916
  • 17
  • 5
  • You add things to `keyHolder2`, then you loop over `validKeys2` - which is empty - then you print the zero returned by `isinRangeTest`. – molbdnilo Apr 05 '22 at 13:27
  • Also, [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – molbdnilo Apr 05 '22 at 13:28
  • I just turned isinRangeTest to a prototype and it still won't do it. I think I am just having a problem taking the valid items from keyHolder2 and putting them in validKeys2. – krainey916 Apr 05 '22 at 13:31
  • There is no place where you are trying to do that, so its being unsuccessful isn't very surprising. Did you intend to call `validPasstest` at some point? (You should read about reference arguments in your favourite C++ book, by the way.) – molbdnilo Apr 05 '22 at 14:45

0 Answers0