Thread: Problem with counting

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Problem with counting

    I'm trying to write a program that reads a .txt file and counts the number words, lines, and paragraphs. Im using a whole blank line as a way to count paragraphs. This is what I have so far however it only gives me an output for word count but it's wrong.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In your code:
    Code:
    // Jeremy Jatindranath -- Project 4
    #include <iostream>
    #include <fstream>
    #include <cctype>
    
    using namespace std;
    
    void initialize(int wordCount, int lineWordCount, int lineCount, int paragraphCount);
    void processBlank(ifstream& inFile, ofstream& outFile, char& ch, int lineWordCount);
    void copyText(ifstream& inFile, ofstream& outFile, char& ch);
    void updateCount(int wordCount, int lineWordCount, int lineCount, int paragraphCount);
    void printTotal(ofstream& outtext, int wordCount, int lineCount, int paragraphCount);
    
    int main()
    {
        int wordCount, lineWordCount, lineCount, paragraphCount;
        char ch;
        ifstream inFile;
        ofstream outFile;
        
        inFile.open("inFile.txt");
        
        //tests for open
        if (!inFile)
        {
                    cout << "Can't open file. Please try again." << endl;
                    system ("PAUSE");
                    return 1;
        }
        
        outFile.open("outFile.txt");
        
        initialize(wordCount, lineWordCount, lineCount, paragraphCount);
        
        inFile.get(ch);
        while ( !inFile.eof())
        {
              while (ch != '\n')
              {
                    processBlank(inFile, outFile, ch, lineWordCount);
                    copyText(inFile, outFile, ch);
              }
              updateCount(wordCount, lineWordCount, lineCount, paragraphCount);
              inFile.get(ch);
              if (ch == '\n')
                 outFile << ch;
        }
        
        printTotal(outFile, wordCount, lineCount, paragraphCount);
        inFile.close();
        outFile.close();
        
        system ("PAUSE");
        return 0;
        
    }
                    
    void initialize(int wordCount, int lineWordCount, int lineCount, int paragraphCount)
    {
         wordCount = 0;
         lineWordCount = 0;
         lineCount = 0;
         paragraphCount = 0;
         
    }
    void processBlank(ifstream& inFile, ofstream& outFile, char& ch, int lineWordCount)
    {
         while (ch == ' ' && ch != '\n')
         {
               outFile << ch;
               inFile.get(ch);
         }
         if (ch != '\n')
            lineWordCount++;
    }
    void copyText(ifstream& inFile, ofstream& outFile, char& ch)
    {
         while (ch != ' ' && ch != '\n')
         {
               outFile << ch;
               inFile.get(ch);
         }
         if (ch == '\n')
            outFile << ch;     
    }
    void updateCount(int wordCount, int lineWordCount, int lineCount, int paragraphCount)
    {
         wordCount = wordCount + lineWordCount;
         if (lineWordCount = 0)
            paragraphCount++;
         else
             lineCount++;
         lineWordCount = 0;
    }
    void printTotal(ofstream& outFile, int wordCount, int lineCount, int paragraphCount)
    {
         if (wordCount == 0)
            paragraphCount = 0;
         outFile << endl;
         outFile << "The number of words are: " << wordCount << endl;
         outFile << "The number of lines are: " << lineCount << endl;
         outFile << "The number of paragraphs are: " << paragraphCount << endl;
    }
    change this line
    Code:
    int wordCount, lineWordCount, lineCount, paragraphCount;
    to:

    Code:
    int wordCount = 5, lineWordCount = 6, lineCount = 7, paragraphCount = 8;
    After this line:

    Code:
    initialize(wordCount, lineWordCount, lineCount, paragraphCount);
    add

    Code:
        cout << wordCount << " " << lineWordCount << " " <<  lineCount << " " << paragraphCount << endl;
        cin.get();
    What does this printout?

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    it puts out '5 6 7 8' in a cmd prompt window. So far I got it to output my infile into an output.txt. Its giving an output for how many words are in it but its wrong and im getting an output of 0 for both number of lines and number of paragraphs

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Ok, but what I am pointing out is you are not initializing the variables in your initialize() function.

    I would suggest that you always initialize your variables when you declare them. And there by deleting the initialize() function.

    Next, you are checking the status of opening your input file (thats good). Why are you not checking that you output file opened correctly?

    Next, I am going to suggest that you switch from using infile.get() to getline. This will do several things. First it will make counting lines, characters, and paragraphs much easier.

    Have you covered std::string yet? Are you allowed to use them in this program?

    Jim

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    im supposed to use the initialize function. and also i wasnt told to check for a successful open for the output. would i really have to if i see that its outputting data? ill try the getline and as for the std::string, it wasnt specified in my instructions. it said to use char. thanks

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Then to use the initialize function you must pass the variables by reference
    Code:
    void initialize(int &wordCount, int &lineWordCount, int &lineCount, int &paragraphCount);
    It is good practice to check for successful opening of all files. This way when something goes wrong you at least know that the file is at opening correctly.

    Also you should not use eof() to as a check of your loop. Use the return values of infile.get(ch), or getline(). There is a FAQ on why not to use eof() here Cprogromming.com FAQ.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. counting problem
    By javani in forum C Programming
    Replies: 16
    Last Post: 04-03-2007, 11:25 PM
  4. Segmentation Fault Problem: Urgent Help
    By bodydrop in forum C Programming
    Replies: 3
    Last Post: 05-05-2006, 08:02 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM