-1

This program should read a paragraph from a text file provided by the user and then store each line in a ragged char array and count the total number of words and lines of the paragraph then display the result.

I can't figure out why does the number of lines keep giving me the result of 3 and why the number of words is always missing 2 words.

please help and keep in mind that I'm no professional just started learning c++ recently.

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int main()
{
    ifstream infile;//declarations
    char filename[45];
    char **data = 0;
    char **temp = 0;
    char *str = 0;
    char buffer[500], c;
    int numoflines = 0, numofwords = 0;

    cout << "Please enter filename: ";//prompt user to enter filename
    cin >> filename;

    infile.open(filename);//open file

    if (!infile)//check if file was successfully opened
    {
        cout << "File was not successfully opened" << endl << endl;//display error message
        return 0;
    } //end of if

    data = new char*[numoflines];

    while (!infile.eof())
    {
        temp = new char*[numoflines + 1];

        for (int i = 0; i < numoflines; i++)
        {
            infile.getline(buffer, 500);//read line
            for (int i = 0; i < strlen(buffer); i++)//count number of words in line
            {
                c = buffer[i];
                if (isspace(c))
                    numofwords++;
            }
            str = new char[strlen(buffer) + 1];//allocate a dynamic array
            strcpy(str, buffer);
            data[i] = str;
            temp[i] = data[i];
        }//end of for

        infile.getline(buffer, 500);
        for (int i = 0; i < strlen(buffer); i++)//count number of words in line
        {
            c = buffer[i];
            if (isspace(c))
                numofwords++;
        }

        temp[numoflines] = new char[strlen(buffer) + 1];
        strcpy(temp[numoflines], buffer);

        delete[] data;
        data = temp;
        numoflines++;
    }

    cout << "Number of words: " << numofwords << endl;
    cout << "Number of lines: " << numoflines << endl;

    return 0;
}
Julian Declercq
  • 1,536
  • 3
  • 17
  • 32
  • For `ifstream::getLine` the default delimiter is '\n'. I guess your text contains 3 paragraphs and it contains only 3 '\n' characters, that's why you get 3 as the number of lines. – sanastasiadis Nov 11 '16 at 11:44
  • For the number of words, I guess the problem is that you check for the space character. Maybe checking for space `OR` period '.' would be better. – sanastasiadis Nov 11 '16 at 11:49
  • Consider "This string". It has two words, but *many* spaces. Consider also "this string" - it has two words and only one space. Finally "what-about-this"? Is it one word or three? (Oh, don't forget about "" - that is zero words). – Martin Bonner supports Monica Nov 11 '16 at 12:25
  • See [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert. Note in particular the suggestion to split the program into lots of very small functions with well defined purposes. As it stands, you have just one function, so it is hard to see what is wrong. – Martin Bonner supports Monica Nov 11 '16 at 12:26
  • You should also [edit] your question to include a least one sample file. – Martin Bonner supports Monica Nov 11 '16 at 12:27

1 Answers1

0

The concept of number of lines is a viewing concept only. It's not encoded into the file. The following single paragraph can be displayed on one line or 16 lines depending upon the size of the editor window:

enter image description here

enter image description here

If you wanted to specify a char width of the window, lines could be calculated from that, but as is, paragraphs and wordcount are as good as you will do. And given a successfully opened ifstream infile that is fairly simple to obtain:

auto numoflines = 0;
auto numofwords = 0;
string paragraph;

while(getline(infile, paragraph)) {
    numofwords += distance(istream_iterator<string>(istringstream(paragraph)), istream_iterator<string>());
    ++numoflines;
}

cout << "Number of words: " << numofwords << "\nNumber of lines: " << numoflines << endl;

NOTE:

Visual Studio supports inline construction of an istringstream so if you're not using that you'll need to construct on a separate line.

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288