1

The scope of the problem is this:

Search an array of string type for a value of string type that was read in from a text file and print the number of occurrences and print the number of lines that keyword occurs in.

The expected output is:

Keyword: union

Total # of keyword occurrences: 87
Total # of lines with occurrences: 29

The function I have written for this is below.

void search(int IDs[], string posts[], int size)
{
    string keyword = "";
    int postCount = 0;
    int lineCount = 0;
    int pos = 0;
    bool found = false;
    cout << "Enter keyword: " << endl;
    cin >> keyword;
    for(int i = 0; i < size; i++)
    {
        while((pos >= 0 && pos <= size))
        {
            pos = posts[i].find(keyword,pos);
            lineCount++;
            pos++;

        }
        if((pos >= 0 && pos <= size-1))
        {
            postCount++;
        }
    }
    cout << "Total # of Occurances: " << lineCount + postCount << endl;
    cout << "Total # of Posts: " << postCount << endl;
}

I ran the code above and got the output below:

Keyword: union

Total # of keyword occurrences: 1
Total # of lines with occurrences: 0

I asked my professor multiple times on what to do, and he told me that I must find the keyword in each line and then start at the pos+1 in the line to continue to find the next occurrences efficiently and keep track of how many times I "find" the keyword, and if I do not find it then do not count the line at all.

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Bowen
  • 55
  • 8
  • 1
    "I asked my professor multiple times on what to do, and he told me that I must find the keyword in each line and then start at the pos+1 in the line to continue to find the next occurrences..." Sounds like good advice :) Your code doesn't match your output, so we're not sure where you're "stuck". SUGGESTION: check for "not found" inside your while loop... – FoggyDay Mar 05 '20 at 01:08
  • 2
    Use getline to read a file line by line. – Arundeep Chohan Mar 05 '20 at 01:19
  • @arundeepchohan I already read the file. I am searching the array of strings now. – Bowen Mar 05 '20 at 01:28
  • 1
    If you used getline instead you could have broken it down using split and then did a count.I have no idea how your array of strings will know it's the next line. – Arundeep Chohan Mar 05 '20 at 01:30
  • each call of find returns one of two values ... you seem to be ignoring (not handling) one of them. – 2785528 Mar 05 '20 at 01:34
  • @2785528 find returns either string::npos or -1 for Mac and it returns the position of keyword that is found. – Bowen Mar 05 '20 at 01:35
  • 1
    so, where (in your code) do you handle npos? – 2785528 Mar 05 '20 at 01:36
  • @2785528 while the pos is between any valid points we continue to find the the keyword in the line. the pos starts at 0, and it will be -1 if not found Mac OS X and then it will NOT satisfy that while loop anymore. – Bowen Mar 05 '20 at 01:38
  • It looks like if a line does not have the keyword (at all), pos is assigned npos. So, where does pos reset to 0 when your for loop steps to the next element of posts? – 2785528 Mar 05 '20 at 01:44
  • @2785528 Let me try to edit the code to see if that will work. I will let you know. – Bowen Mar 05 '20 at 01:45
  • @2785528 I get wrong output, I attempt to find in the while loop condition and it returns the wrong output. I find the keyword 56 times, it is wrong. – Bowen Mar 05 '20 at 01:47
  • Your 'find' statement triggers 2 warnings. Add -Wconversion and -Wsign-conversion to your compile, then solve the warnings. – 2785528 Mar 05 '20 at 02:56
  • [MCVE] 's should compile. does this post compile? – 2785528 Mar 05 '20 at 03:03
  • Did you find, in your code snippet, where 'pos' is reset to 0 for each strring (of posts) to be searched? A 'patch' to try might be to add to the bottom of the 'for loop': pos = 0; – 2785528 Mar 05 '20 at 03:06
  • FYI: From https://en.cppreference.com/w/cpp/string/basic_string/npos: "Although the definition uses -1, size_type is an unsigned integer type, and the value of npos is the largest positive value it can hold, ..." – 2785528 Mar 05 '20 at 15:33
  • Because of its placement in the function parameters, I would guess that 'size' is the c-style number of element in 'posts'. Thus, the first for-loop (where size is first used) is apparently stepping through the elements of posts. HOWEVER, pos, set by find, is used as an index into a single element, so there, 'size' (the array size) is misapplied as if it was the element size. I think these mistakes would have been avoided by using C++ "vector posts;", then no parameter 'size' is needed (by using posts.size()), and the confusion with string length could have been avoided. – 2785528 Mar 05 '20 at 16:34
  • @2785528 Yeah I was using the wrong size variable. – Bowen Mar 07 '20 at 21:02

0 Answers0