Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am doing a project that finds certain words in a file and also counts the numb

ID: 3641479 • Letter: I

Question

I am doing a project that finds certain words in a file and also counts the number of words, charactors, sentences, and lines in the data file. 

I need to know how to search a file for a word and also display the line that the word was found on.

The basic algorithm is as follows:

1. User enters in file name
2. Program opens file
3. A menu is displayed with one option being Word Search
(we will assume they select the word search option)
4. User enters in a word they want to search for
5. The program searches the data file for the desired word, and then prints the number of times the word appears in the file, as well as the line numbers that the words appear in.

Output example

(Green parts are done, red parts are what I am stuck on)

Welcome to the system of “Text Analyzer”.
Enter the file name to analyze: project7.dat
*****************************************
1. count the number of words
2. count the number of lines
3. count the number of characters
4. count the number of sentences
5. search for a word
*****************************************
Enter your option: 5

Enter the word to search for: the

The word "the" appeared 2 times in the file. It appeared on line(s) 1, 3.

 

Source Code:

#include<iostream>
#include<fstream>       // File stream library
#include<string>

using namespace std;


void lineCounter(ifstream &);   // Function prototypes
void wordCounter(ifstream &);
void charCounter(ifstream &);
void sentCounter(ifstream &);
void wordSearch(ifstream &);

int main()                              // Main function

        {
        string fileName;                // Name of file
        int menuChoice = 0;                     // Menu choice options

        int numLines, numWords, numChars, numSentences;         // Counters
        string searchWord;              // Word to be searched for

        ifstream inFile;

        cout << "----------- Text Analyzer System ----------- ";
        cout << "Enter the name of the file to be analyzed: ";

        getline(cin, fileName);         // File name input
        inFile.open(fileName.c_str());  // Opens the specified file


        while (inFile.fail())   // File name input validation loop
          {
          inFile.close();
          inFile.clear();

          cout << "!! File opening error !! ";
          cout << "Please enter the file name again: ";
          getline(cin, fileName);               // File name input

          inFile.open(fileName.c_str());
          }

        cout << "  Analyzer Options Menu ";
        cout << "  ********************* ";
        cout << "  1. Display line count ";
        cout << "  2. Display word count ";
        cout << "  3. Display character count ";
        cout << "  4. Display sentence count ";
        cout << "  5. Word search ";
        cout << "  6. Exit program ";

        cout << "Type the number of your choice: ";
        cin >> menuChoice;


        if (menuChoice == 1)
          lineCounter(inFile);

        else if (menuChoice == 2)
          wordCounter(inFile);

        else if (menuChoice == 3)
          charCounter(inFile);

        else if (menuChoice == 4)
          sentCounter(inFile);

        else if (menuChoice == 5)
          wordSearch(inFile);

        else if (menuChoice == 6)
          cout << "Thank you for using the Text Analyzer System.  Goodbye. ";

        inFile.close();

        }


void lineCounter(ifstream &file)

        {
        // Already done
        }

void wordCounter(ifstream &file)

        {
        // Already done
        }

void charCounter(ifstream &file)

        {
        // Already done
        }

void sentCounter(ifstream &file)

        {
        // Already done
        }

void wordSearch(ifstream &file)

        {
        // This is the function I need help with
        }

Explanation / Answer

Please rate...

Also include the file <cstring>

that is #include<cstring>

void wordSearch(ifstream &file)
{
    char a[50];
    int c=0;
    int ar[100];
    while(c<100)
    {
        ar[c]=0;
        c++;
    }
    c=0;
    int i,j,k=0,l;
    fflush(stdin);
    cout<<" Enter the word to search for: ";
    gets(a);
    while(file)
    {
        c++;
        char temp[1000],t[50];
        i=0,j=0,l=0;
        file.getline(temp,1000);
        while(temp[l]!='')
        {
            l++;
        }
        for(i=0;i<=l;i++)
        {
            if(temp[i]!=' ')
            {
                t[j]=temp[i];
                j++;
            }
            else if(temp[i]==' ' || temp[i]==' ')
            {
                j=0;
                if(strcmp(t,a)==0)
                {
                    ar[k]=c;
                    k++;
                }
                int d=0;
                while(d<50)
                {
                    t[d]='';
                    d++;
                }
            }
        }
    }
    cout<<" The word ""<<a<<"" appeared "<<k;
    cout<<" times in the file. It appeared on line(s) ";
    for(i=0;ar[i]!=0;i++)
    {
        cout<<ar[i]<<", ";
    }
}