-2

I am just starting out learning C++. I have been working on this program for several days with no success. I would sincerely appreciate any advice or guidance you could provide. I apologize in advance for any bad form, and I also thank you in advance for taking the time to help me.

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

int main(){
ifstream inFile;
string fileName, text;
char reply, enter, ch;
int character, word, counter=0;


cout<< "Please enter the file name ";
getline(cin, fileName);

//Opens the file
inFile.open(fileName.c_str());

while(!inFile)
{
cout<< "\nNo such file. Enter again. ";
getline(cin, fileName);
inFile.open(fileName.c_str());
}

while(1)
{
    cout<<"\nCount word or character? (w/c): ";
    cin>> enter;

    if(enter=='w'){
        cout<<"\nEnter word to search: ";
        cin>>text;
        inFile.seekg(0, inFile.beg);
        counter=0;
        int i=0;
        while(inFile.get(ch))
        {
            if(ch == ' ')
            {
                i=0;
            }
            else if(ch == text[i])
            {
                i++;
                if(i == text.length())
                {
                    counter++;
                    i=0;
                }
            }
        }
    }
    else if(enter=='c')
    {
        cout<<"\nEnter character to search: ";
        cin>> text;
        counter = 0;
        while(inFile.get(ch))
        {
            if(ch == text[0])
                counter++;
        }
    }
    cout<< "\nNumber of "<< text[0]<< " in file is " << counter;
    cout<< "\nWant to proceed this file again? (y/n) ";
    cin>> reply;
    if (reply == 'n')
        break;
}

inFile.close();
cout<<"\nThank for trying";

return 0;

}
  • And the question is.... ? – P0W Aug 03 '14 at 07:56
  • 3
    Your question is worded very politely, but yes, it will be easier to help you if you point out exactly what the question is. For example, are you segfaulting? Getting incorrect results? If so, what exactly is the incorrect result? – merlin2011 Aug 03 '14 at 07:57
  • Hi, the question is on the wrong code part that to count the quantity of certain character or word in a text file entered by the user – crystalreed Aug 03 '14 at 08:02
  • 1
    Well, there are some logic errors obviously. Start by asking yourself, **what am I doing with `str` after I read it in with `getline(inFile,str)`?** – David C. Rankin Aug 03 '14 at 08:07

3 Answers3

1

The first obvious problem is that you need curly braces around the body of a multi-line if block.

    if(count=='w') {
        cout<<"\nEnter word to search: ";
        cin>> search;
    }

    else if(count=='c') {
        cout<<"\nEnter character to search: ";
        cin>> search;
    }

The second problem is that you are using an unintialized character buffer to read user input. In this case it simplest to just change search to a c++ string.

string search;

The third problem is that you declare count as an int instead of a char, so cin will try to parse the user input as an integer instead of a character when you write cin >> count. The same problem occurs with reply. This can be corrected by changing the declaration to char.

int characters, words;
char count, reply;

Now, in terms of actually reading a file into memory correctly in C++, take a look at this answer.

Community
  • 1
  • 1
merlin2011
  • 63,368
  • 37
  • 161
  • 279
0

to start with, you can ommit braces in if statement only when inside your "if" there would be only one instruction inside. here you have two:

if(count=='w')
cout<<"\nEnter word to search: ";
cin>> search;

else if(count=='c')
cout<<"\nEnter character to search: ";
cin>> search;

therefore compiler wouldn't recognise the "else if" part and will crash. add braces.

if(count=='w') {
    cout<<"\nEnter word to search: ";
    cin>> search;
}

else if(count=='c') {
    cout<<"\nEnter character to search: ";
    cin>> search;
}

next bug: if user inputs wrong file name, then your program terminates. user should have ability to re-enter file name, so you should read that file name in some loop, for example:

ifstream f;
cout << "Enter the input file name: ";
string filename;
cin >> filename;
f.open(filename.c_str());
while (!f) {
    cout << "no such file. enter again. ";
    cin >> filename;
    f.open(filename.c_str());
}

example logic for counting words:

string sw, ss;
cout << "type word to count: ";
cin >> sw;
int cc,cnt = 0;

while (f.good()) {
    cc = f.get();
    if (cc != ' ') {  // if char that was read is different than space
        ss += cc;     // append char to string
    }
    else {
        ss = "";      // if char is equal to space, flush the string
    }
    if (sw == ss) {   // compare actual set of chars with given word
        cnt++;        // if equal, increment counter
    }
}

example logic for counting chars: just read in loop one char and compare it with given char by user.

macfij
  • 2,868
  • 1
  • 16
  • 23
0

Demonstration: The logic I used in the following code for searching a specific word in the whole file is that if a user has entered a word (say user entered search = "you") then our program will start searching the first letter of your search word and when the first letter matched (at i=0 in search[i]) then we'll check the next character by incrementing i. If the next character is also matched then we'll keep incrementing like before and finally when the size of search becomes equal to i (in our case as size = 3 for 'you' so the i will be set to 0 and we'll say the whole word is matched so we incremented in counter for word). Same logic is for one character and because of this same logic your code becomes easier. IMP: For this logic you'll have to close and reopen the file so that your file pointer points to the start of your file every time you re read it. (or if you don't want to reopen the file then you can use 'inFile.seekg (0, is.beg);' when you start your while(1) loop. Hope you understand.

int main(){
ifstream inFile;
string fileName, text;
char reply, enter, ch;
int character, word, counter=0;


cout<< "Please enter the file name ";
getline(cin, fileName);

//Opens the file
inFile.open(fileName.c_str());

while(!inFile)
{
cout<< "\nNo such file. Enter again. ";
getline(cin, fileName);
inFile.open(fileName.c_str());
}

while(1)
{
    cout<<"\nCount word or character? (w/c): ";
    cin>> enter;

    if(enter=='w'){
        cout<<"\nEnter word to search: ";
        cin>>text;
        inFile.seekg(0, inFile.beg);
        counter=0;
        int i=0;
        while(inFile.get(ch))
        {
            if(ch == ' ')
            {
                i=0;
            }
            else if(ch == text[i])
            {
                i++;
                if(i == text.length())
                {
                    counter++;
                    i=0;
                }
            }
        }
    }
    else if(enter=='c')
    {
        cout<<"\nEnter character to search: ";
        cin>> text;
        counter = 0;
        while(inFile.get(ch))
        {
            if(ch == text[0])
                counter++;
        }
    }
    cout<< "\nNumber of "<< text[0]<< " in file is " << counter;
    cout<< "\nWant to proceed this file again? (y/n) ";
    cin>> reply;
    if (reply == 'n')
        break;
}

inFile.close();
cout<<"\nThank for trying";

return 0;

}

Ali Mohyudin
  • 232
  • 2
  • 10