Thread: Counting words by character

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    1

    Counting words by character

    OK new to C++ and want to learn it, having problem with
    code that inputs file and counts the words in the file. I am doing this by character, I know I can do it by string easier! just looking for
    direction.

    File opens, Loop works, count works but is not correct?
    As I parse a page do I have to make any other excludes for line breaks, \n or other ......

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cassert> 
    #include <fstream> 
    #include <string>
    
    using namespace std;
     
    int main()
    {
        int wordCount;
        char prevChar;
        char currChar;
        ifstream inFile;  
        string fileName;
        cout << "Enter the input file name (quit to exit): ";
        cin >> fileName;
        while (fileName != "quit"){  
                inFile.open(fileName.c_str());
                assert(inFile);
                wordCount = 0; 
                inFile.get(prevChar);
                inFile.get(currChar);
        while(inFile.get() != '\n') {
               if (currChar == '  ' || prevChar != '  ')
               wordCount++;   
               prevChar = currChar;
               inFile.get(currChar);  
        }      
        cout << "There are " << wordCount << " words in this " 
        << fileName << endl << endl;
        inFile.close();
        inFile.clear();
        cout << '\a';
              
        cout << "Enter the input file name (quit to exit): ";
        cin >> fileName;    
        }
        system("PAUSE");
        return EXIT_SUCCESS;
        
    }
    Last edited by batc5; 02-15-2005 at 11:16 PM. Reason: Define

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Quote Originally Posted by batc5
    The program seems to work if file has all words add a number
    which I want to count as word and bad count?
    ?????????? That right there makes no sense whatsoever. Try to use better grammar.
    My computer is awesome.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Aside from indentation, I don't see any major problems with your code. If you're having problems getting your program to work, tell us specifically what is happening and what should be happening - it'll be easier to find the problem that way.

    That right there makes no sense whatsoever. Try to use better grammar.
    I've seen much worse, cerin. Don't worry too much about this if English isn't your first language. Do your best, but the important thing is to just ask specific questions - it's the best way to get help around here.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I have to agree with cerin, but I did notice one error:
    Code:
    if (currChar == ' ' || prevChar != ' ')
               wordCount++;
    Let's say your file contains:

    apple

    so prevChar='a' and currChar='p'. In the if condition, prevChar does not equal a blank, so wordcount is incremented by 1, and every time you go through the while loop wordcount is similarly incremented by 1 even though there is only one word in the file.
    Last edited by 7stud; 02-15-2005 at 11:15 PM.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    here's some basic code to help you with (what I think is) what you want:
    Code:
    #include<fstream>
    #include<iostream>
    
    int main()
    {
        char*word=new char[20]; //a character array (C-type string)
        int count=0;            //to hold the number of words
        
        std::ifstream infile("temp.txt");   //open the file
        
        if(!infile)                         //if the file doesn't open
        {
            std::cerr<<"File Input Error";  //output an error
            std::cin.get();                 //wait for ENTER
            delete[]word;                   //deallocate the character array
            exit(1);                        //exit with a fail status
        }
        
        while(infile>>word) //keep going as long as there are more words
            count++;        //for each word, increase the count by one
            
        infile.close();                             //close the file
        std::cout<<count<<" words were counted.";   //output the count
        std::cin.get();                             //wait for ENTER
        delete[]word;                               //deallocate the char array
        return 0;                                   //return with success status
    }
    you'll have to modify it to fit your needs though, because I didn't look at how you had your string-based solution fitted into your program.
    Last edited by major_small; 02-15-2005 at 11:18 PM. Reason: more info...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. need help with 'character counting from a .txt file'
    By master_vst in forum C Programming
    Replies: 5
    Last Post: 11-09-2008, 02:17 PM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. counting words in a text file...
    By dalguy2004 in forum C Programming
    Replies: 4
    Last Post: 09-23-2003, 10:53 AM
  5. counting words in a text file...
    By flightsimdude in forum C Programming
    Replies: 10
    Last Post: 09-19-2003, 07:02 PM