#include <iostream>
#include <fstream>
using namespace std;
void countWords(fstream &myText, int word);
void countLines(fstream &myText, int lines);
void wordLength(fstream &myText, int length);


int main()
{
	char textLine[100];
	int lines = 0, word = 0;
	fstream myText;

	myText.open("myText.txt", ios::in | ios::out);
	if (!myText)
	{
		cout << "Cannot open file - myText.txt" << endl;
		exit(1);
	}
	cout << "Enter some lines of text. (Blank to exit)" << endl;
	cin.getline(textLine, 100);
	while (textLine[0] != '\0')
	{
		cout << "Enter another line of text." << endl;
		cin.getline(textLine, 100);
	}
 
	myText.seekg(0);
	cout << "The text in this file is " << endl;
	myText.getline(textLine, 100);
	countWords(myText, word);
	countLines(myText, lines);

    myText.close();
	return 0;
}

void countWords(fstream &myText, int word)
{
	char ch, previousChar = NULL, blank = ' ';
	int numberChars = 1;
	while (!myText.eof())
	{
		myText.get(ch);
		cout << ch << endl;
		numberChars++;
		if (ch == blank && previousChar != blank)
		word++;
	}
	cout << "There are " << word + 1 << " word(s) in this file." << endl;
}

void countLines(fstream &myText, int lines)
{
	char ch, endOfLine = '\n';
	int numberChars = 1;
	while (!myText.eof())
	{
		myText.get(ch);
		cout << ch << endl;
		numberChars++;
		if (ch == endOfLine)
			lines++;
	}
	cout << "There are " << lines - 1 << " line(s) in this file." << endl;
}

Hello,

I'm trying to make a program which outputs the text in a text file and also
calculate the number of lines in the file, the number of words and the average
word length. I haven't tried to do the average word length yet, so my program
ignores that for the time being. My program compiles, but when I debug, one character is printed per line and
I'd like for it to print as it appears in the text file. Here's my code, and
I'm sorry if I did any of my posting wrong, this is my first time and I'm also
still new to C++ programming.

Edited 6 Years Ago by Nick Evan: Snippet-&gt;thread

Oops, I did that very wrong, sorry!! I'll repost if you want me to.

Oops, I did that very wrong, sorry!! I'll repost if you want me to.

NVM, common rookie mistake. I'm sure a mod will fix it soon enough, just be mindful in the future. Snippets are intended to be useful little chunks of code that you would like to share with others. Otherwise, just use [code] ...code tags... [/code] in a regular post.

In your loop(s), you display the characters as they are read, which is probably fine. The problem is that you send an endl after each one. Wait until a newline is detected, then send the endl.

Edited 6 Years Ago by Fbody: n/a

OK, I fixed that problem (just took out the endl). I have a new problem though. My functions don't work properly. The word count function outputs the wrong number. Do I need to have another condition in the while loop for new lines? Also the line count doesn't work at all and I'm not sure why (it says there are -1 lines which I know is not true).

When the function countLines() is called the file's get pointer is already pointing at the end of the file. You need to reset the file pointer to the beginning of the file. You'll need to use seekg() again.

Edited 6 Years Ago by Fbody: n/a

I fixed the word count. I'm not sure where the seekg() needs to go. (in the countLines function, or in the main function?) I understand what you mean though, this may just be a case of me having starred at this for too long now. Here's my program:

#include <iostream>
#include <fstream>
using namespace std;
void countWords(fstream &myText, int word);
void countLines(fstream &myText, int lines);
void wordLength(fstream &myText, int length);


int main()
{
	char textLine[100];
	int lines = 0, word = 0;
	fstream myText;

	myText.open("myText.txt", ios::in | ios::out);
	if (!myText)
	{
		cout << "Cannot open file - myText.txt" << endl;
		exit(1);
	}
 
	myText.seekg(0);
	cout << "The text in this file is: " << endl;
	myText.getline(textLine, 100);
	countWords(myText, word);
	countLines(myText, lines);

    myText.close();
	return 0;
}

void countWords(fstream &myText, int word)
{
	char ch, previousChar = NULL, blank = ' ', endOfLine = '\n';
	int numberChars = 1;
	while (!myText.eof())
	{
		myText.get(ch);
		cout << ch;
		numberChars++;
		if (ch == blank && previousChar != blank || ch == endOfLine)
		word++;
	}
	cout << "\n\nThere are " << word + 1 << " word(s) in this file." << endl;
}//Ignores the first line, but otherwise counts the words correctly.

void countLines(fstream &myText, int lines)
{
	myText.seekg(0);
	char ch, endOfLine = '\n';
	int numberChars = 1;

	while (!myText.eof())
	{
		myText.get(ch);
		cout << ch;
		numberChars++;
		if (ch == endOfLine)
		{
			lines++;
		}
	}
	cout << "There are " << lines << " line(s) in this file." << endl;
}

IMO, it should occur once at the beginning of each function, which it looks like you did with countLines(). I would lose Line 22 and add it to countWords() as well...

OK, I tried that (it did fix one part of of it), but the count lines still doesn't work =/

This article has been dead for over six months. Start a new discussion instead.