Thread: Homework help please

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    10

    Homework help please

    So the homeworke ask to open a text file and count the words in that file. I got this part right but the second part im having problems with which ask to count the occurrence of a particular word in that file. the code i wrote always return 0.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
        ifstream inFile;
        string fileName;
        string search;
        string word;
        int count = 0;
    
        cout << "Please input the filename :  ";
        getline(cin, fileName);     // input the the file path 
    
        inFile.open(fileName.c_str());
    
        while (!inFile)      // loop to ask the user to input the filename again if the file can not be found
        {
            cout << "ERROR:" << fileName << " can not be opened!" << endl;
            cout << "Please input the filename :";
            getline(cin, fileName);
            inFile.open(fileName.c_str());
        }
    
        while (!inFile.eof())   // while loop to count the words 
        {
            inFile >> word;
            count++;
        }
    
        cout << "Number of white-space seperated words = " << count - 1 << endl;      // output the number of words in the file
     
    
    
        cout << " input the word to search  :" ;
        cin >> search;
    
        int wordcount = 0;
    
        while (!inFile.eof()){
            if (word == search){
                ++wordcount;
            }
        }
      
        cout << " The word occured  :" << wordcount << endl;
    
       inFile.close();
    
        return 0;
    }
    Last edited by Feras Kakish; 02-20-2015 at 10:52 AM.

  2. #2

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    10
    Please i need someone to point out whats wrong with the code i wrote.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    When you counted the number of words you read the file until the stream reached eof(). This means that the stream is now in an error state at eof(). You'll need to go back to the beginning of the file and depending on the version of the C++ standard you happen to be using you may need to reset the stream state first.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework, please help!
    By FandaR in forum C Programming
    Replies: 4
    Last Post: 04-30-2009, 08:59 AM
  2. C homework
    By wilson5182004 in forum C Programming
    Replies: 6
    Last Post: 03-01-2009, 02:21 PM
  3. help with homework
    By abhiii in forum C Programming
    Replies: 2
    Last Post: 02-13-2009, 01:48 PM
  4. Help on Homework
    By Sentiax in forum C Programming
    Replies: 13
    Last Post: 02-04-2009, 12:33 AM
  5. Homework =)
    By Raeliean in forum C++ Programming
    Replies: 9
    Last Post: 07-16-2005, 10:27 PM