+ Reply to Thread
Results 1 to 5 of 5

Thread: Help with word counter.

  1. #1
    Programming Professional so1i is an unknown quantity at this point so1i's Avatar
    Join Date
    Sep 2009
    Posts
    267

    Help with word counter.

    Hey,

    I have been attempting to learn some C++ (very novice stuff) but I was trying to write a couple of simple programs to try and learn the basics.

    I've started on a word counter, with a word finder attached. You search for a word, and it tells you how many times it is included. This was just to try and learn some of the basic syntax, I'm sure it could be implemented in a better way

    Here is my attempt at code:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
        ifstream myfile ("eg.txt",ios::in);
        if (myfile.is_open())
        {
            string x;
            string readStr;
            string searchWord;
            int noWords = 0;
            while (myfile >> x)
            {
                readStr = readStr + " " + x;
                noWords++;
            }
            myfile.close();
            cout << "The number of words in this file is: ";
            cout << noWords;
            cout << "\n\nSearch for word?\n";
            cin >> searchWord;
            findWord(searchWord);
        }
        else cout << "Cannot open file";
        return 0;
    }
    
    int findWord(string searchStr)
    {
        ifstream myfile ("eg.txt",ios::in);
        if (myfile.is_open())
        {
            string x;
            string readStr;
            int noFound = 0;
            while (myfile >> x)
            {
                if (x==searchStr)
                {
                    noFound++;
                }
            }
            myfile.close();
            return noFound;
        }
        else
        {
            cout << "Cannot open file";
            return 0;
        }
    }
    Now, I have a couple of questions about this. Firstly, it won't run as it says the function findWord is not properly declared, but I can't quite understand why. Secondly, I have done it this way, using only iostream, so was wondering what the advantages, if there are any, were of using stdio and scanf/printf rather than this way?

    Those questions might make no sense, I really am just learning the basics atm, so any help would be greatly received.

    Thanks a lot!

  2. #2
    Newbie Naeth is an unknown quantity at this point Naeth's Avatar
    Join Date
    Oct 2009
    Location
    Manchester
    Posts
    5

    Re: Help with word counter.

    First, findWord isn't seen by main because you haven't declared it. You can either put a prototype above it (int findWord(string); ) or you can move the whole function above main.

    Secondly, iostream is C++ and stdio.h is C. Seeing as you are using std::string you don't want to start mixing it with C functions.
    Last edited by Naeth; 10-18-2009 at 01:57 AM. Reason: Stupid smily

  3. #3
    Programming Professional so1i is an unknown quantity at this point so1i's Avatar
    Join Date
    Sep 2009
    Posts
    267

    Re: Help with word counter.

    Quote Originally Posted by Naeth View Post
    First, findWord isn't seen by main because you haven't declared it. You can either put a prototype above it (int findWord(string); ) or you can move the whole function above main.

    Secondly, iostream is C++ and stdio.h is C. Seeing as you are using std::string you don't want to start mixing it with C functions.
    Ah great, thanks! I didn't realise that about C++, and the order of functions being important. Cheers

    So for C++, I'm using the correct way as far as I/O terms go?

  4. #4
    Newbie Naeth is an unknown quantity at this point Naeth's Avatar
    Join Date
    Oct 2009
    Location
    Manchester
    Posts
    5

    Re: Help with word counter.

    Yeah. You might want to read this too: bit.ly/1mEmjr

    (Cant post URL's yet. Had to use bit.ly)

  5. #5
    Programming Professional so1i is an unknown quantity at this point so1i's Avatar
    Join Date
    Sep 2009
    Posts
    267

    Re: Help with word counter.

    Thanks very much Appreciate that!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Java: Word Counter
    By chili5 in forum Classes and Code Snippets
    Replies: 2
    Last Post: 09-02-2009, 03:55 AM
  2. Guess The Word.
    By Paradox in forum Java Tutorials
    Replies: 5
    Last Post: 01-16-2009, 07:48 AM
  3. Remove a word from sentence using arrays
    By Hawk1 in forum C and C++
    Replies: 4
    Last Post: 12-09-2008, 12:20 AM
  4. Help! Rlink32 Error
    By Sparky in forum Pascal/Delphi
    Replies: 6
    Last Post: 10-16-2008, 07:31 AM
  5. Dictonary Program
    By programmer 101 in forum Java Help
    Replies: 9
    Last Post: 07-01-2007, 10:39 AM