I am taking entry level C++ course, and I am having trouble with my assignment for this week on Sequential Files. Here is my assignment:
Create a C++ console application that analyzes the contents of a text file.

Your program should count the number of:

lines (hint: use getline)
words (hint: count the spaces)
letters (hint: remember character arrays?)
punctuation marks (period, comma, and semicolons).
Displays that data in a table.

I don't know how to create the code to count the number of lines, words, letters, and punctuation marks in a text file. So for my code I just counted them myself and put a >=. My code runs successfully but I need a better algorithm for the counting of lines, words, etc.
Please help!
Here is a copy of my code:

/*Specification:
Natalie Androl
Lab 7 Exercise 1
This program reads and counts elements in a text file*/

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void TextFile();
void TextCount();
int CountLines(string lines);
int CountWords(string spaces);
int CountLetters(string letters);
int CountMarks(string punctMarks);


int main()
{
    TextFile();
    TextCount();

    return 0;
}
void TextFile()
{
        string lineBuffer;
        ofstream outFileObj;
        outFileObj.open("c:/textFile.txt");
        cout << "Enter text and type 'quit' to end file\n";
        getline(cin, lineBuffer);
        while(lineBuffer != "quit")
        {
                outFileObj << lineBuffer << endl;
                getline(cin, lineBuffer);
        }
        outFileObj.close();
}
void TextCount()
{
    string lines;
    string words;
    string letters;
    string punctMarks;

    int count = 0;
    int count1 = 0;
    int count2 = 0;
    int count3 = 0;

    ifstream inFileObj("c:/textFile.txt");
    if(inFileObj.is_open())
    {
        getline(inFileObj, lines);
        getline(inFileObj, words);
        getline(inFileObj, letters);
        getline(inFileObj, punctMarks);

        while(!inFileObj.eof())
        {
            cout << lines << endl;
            count = count + CountLines(lines);
            getline(inFileObj, lines);

            cout << words << endl;
            count1 = count1 + CountWords(words);
            getline(inFileObj, words);

            cout << letters << endl;
            count2 = count2 + CountLetters(letters);
            getline(inFileObj, letters);

            cout << punctMarks << endl;
            count3 = count3 + CountMarks(punctMarks);
            getline(inFileObj, punctMarks);
        }
        cout << "Number of lines: " << count << endl;
        cout << "Number of words: " << count1 << endl;
        cout << "Number of letters: " << count2 << endl;
        cout << "Number of punctuation marks: " << count3 << endl;

        inFileObj.close();
    }
    else
        cout << "File Error: Open Failed\n";
}

int CountLines(string lines)
{
    int count = 0;

    for(int i = 0; lines[i] != '\0'; ++i)
    {
        if(lines[i] >= 6)
            ++count;
    }
    return count;
}

int CountWords(string words)
{
    int count1 = 0;

    for(int i = 0; words[i] != '\0'; ++i)
    {
        if(words[i] >= 15)
            ++count1;
    }
    return count1;
}

int CountLetters(string letters)
{
    int count2 = 0;

    for(int i = 0; letters[i] != '\0'; ++i)
    {
        if(letters[i] >= 73)
            ++count2;
    }
    return count2;
}

int CountMarks(string punctMarks)
{
    int count3 = 0;

    for(int i = 0; punctMarks[i] != '\0'; ++i)
    {
        if(punctMarks[i] <= 3)
            ++count3;
    }
    return count3;
}
#include<fstream>
#include<cstring>
#include<string>
#include<algorithm>

int line_counter = 0;
int word_counter = 0;
int punct_counter = 0;
int size = 0;
ifstream infile;
string line;

while(getline(infile, line))
{
     line_counter++;

     size = line.size();
     word_counter = count(&line[0], &line[size], ' ');
     word_counter++;

     for(int i=0; i<size; i++)
     {
          if(ispunct(line[i]))
          {
               punct_counter++;
          }
     }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.