Problem counting characters

I'm trying to find the number of link, tags, comments, and characters in an HTML file, I get the correct number of links, tags, and comments when i runthis program, but the number of characters ends up being short of the correct amount of characters by the number of tags there are, so RightNumOfChars - NumOfTags = WrongNumOfChars(the amount the program gives me).

I hope this made sense, but I dont know what i'm doing wrong for the program to subtract the number of tags from the character total

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
	char  ch;
	const char TAG= '<',
		LINK= 'a',
		COMMENT= '!';
        int     tags = 0,
		links = 0,
		comments= 0,
		chars = 0,
	
        string fileName;
	ifstream inFile;

	cout << "Enter a file name: ";
	cin >> fileName;
	
	inFile.open(fileName.c_str());

	while (!inFile)
	{
		inFile.clear();
		cout << endl << "Error opening file.\n",
			cout << endl << "Please enter a file name: ";
		cin >> fileName;
		inFile.open(fileName.c_str());
	}

	cout << "Here's The Text of the File!!" << endl;

	inFile.get(ch);
	cout << ch;

	while (inFile)
	{
		if (ch == TAG)
		{	
			tags++;
			inFile.get(ch);
			cout << ch;
			if ( ch == LINK )
			{
				linkst++;
			}
			else if (ch == COMMENT)
			{
				comments++;
			}

		}

		inFile.get(ch);
		cout << ch;
		chars++;
	}

		cout << "-------------------" << endl,
		cout << "# of Tags: " << tags << endl,
		cout << "# of Comments: " << comments << endl,
		cout << "# of Links: " << links << endl,
		cout << "# of Chars: " << chars<< endl,
		
	inFile.close();
	return (0);
}
Last edited on
When line 42 evaluates to true, you get more input (line 45/46) without a corresponding increment to the variable holding the number of chars read.
Topic archived. No new replies allowed.