1

I want this c++ program to find the the words starting with user entereed letter and print them using a new function.but the thins is that it only finds 1st letter for the second time it runs ina loop and then , i dont know what happens ... I am a beginner please help me! uncomment the line that are necessary

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

void getUserInput(string *filename, string *find)
{
    cout << "file name : ";
    cin >> *filename;
    cout << "required character : ";
    cin >> *find;

}
string* processFile(string fileName, string word, int *t, int *w, string found[])
{
    fstream file;
    int countIn = 0,
        totaal = 0;
    int *count = &countIn;
    int *total = &totaal;
    int i = 0;
    string find;  // the max length of the file should not exceed this value is if does please change it here.
    file.open(fileName, ios::in);
    if (file.is_open())
    {
        while (!file.eof())
        {

            file >> find;
            totaal++;
            if (word == find)
            {
                char a[100];
                int s = find.size();
                for (int j = 0; i < find.size(); j++) 
                {
                    string(1, find[i]);
                }

                found[i] = find;
                i++;
                countIn++;
            }

        }
    }
    else
        cout << "!!!!invalid file name!!!!\n";
    file.close();
    //for (int i = 0, j = 0; i < totaal; i++)
    //{
    //  
    //  cout << find[i] << '\t' << find[i][0] << endl;
    //  
    //  if (word == find[i][0])
    //  {
    //      cout << "i is " << i << endl;
    //      cout << "j is " << j << endl;
    //      cout << "Calculated i and j\n";
    //      found[j] = find[i];
    //      cout << "found[j] " << found[j] << "\nfind[i] " << find[i] << endl;
    //      j++;
    //      countIn++;
    //      cout << "Inside if\n";
    //  }
    //  cout << "outsidenside if\n";
    //}
    *t = *total;
    *w = *count;
    //cout << countIn << endl << totaal << endl;
    //cout << *count << endl << *total<<endl;

    return found;

}
void displayOutput(int total, int count, string wordlist[])
{
    cout << "Total words in the file: " << total;
    if (count != 0)
    {
        cout << "\nTotal " << count << " related words found in the file\n";
        for (int i = 0; i < count; i++)
            cout << i + 1 << "\t" << wordlist[i] << endl;
    }
    else
        cout << "No matching case found\n";



}
int main()
{

    string nameoffile;
    string wordtofind;
    string *ptr1 = &nameoffile;
    string *ptr2 = &wordtofind;
    string foundwords[] = { "" };
    int occur = 0,
        totalWords = 0;
    int *occ = &occur;// occurence of the certain word
    int *tot = &totalWords;// total wods in the file
    getUserInput(ptr1, ptr2);

    processFile(nameoffile, wordtofind, occ, tot, foundwords);  //calling the processing function

    displayOutput(occur, totalWords, foundwords);

    return 0;
}
  • 1
    Why is there any pointer usage in a program that, from your description, doesn't need to use pointers at all? – PaulMcKenzie Apr 28 '20 at 15:08
  • 1
    `while (!file.eof())` is almost certainly [wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Ted Lyngmo Apr 28 '20 at 15:18
  • Prefer to pass `std::string` by reference, or if you are changing the parameter, pass by `const` reference. – Thomas Matthews Apr 28 '20 at 17:22
  • Paul if I don't use pointers how come I'll be able to access the char array - in which the text is retrieved- in the main function or any other body of the function. Given that I cant use a global variable. sorry I didn't mention earlier. – user_not_dead Apr 29 '20 at 15:32

0 Answers0