file analyzing

I have this assignment that reads in files and then analyzes them. I've gotten it to read, count the number of characters, the number of tags, and lines. I can't get it to count the number of comments, links, number of characters in the tags or the percentage of characters in tags. This is an html file by the way.

Here's an example of how the output should be:

Analysis of file
----------------

Number of lines: 9
Number of tags: 8
Number of comments: 1
Number of links: 1
Number of chars in file: 176
Number of chars in tags: 87
Percentage of characters in tags: 49.43%

Here's my code:


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
72
73
74
75
76
77
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	//ifstream inputFile; // File stream Object
	char ch;
	const char LINK_OPEN = 'a';
	const char TAG_OPEN = '<';
	bool link = false;
	int tagCount = 0;
	int charCount = 0;
	int lineCount = 0;
	int linkCount = 0;
	int commentCount = 0;

	string line;
	string filename;		
	ifstream inputFile; 

	cout << "========================================================" << endl;
	cout << "                  HTML File Analyzer" << endl;
	cout << "========================================================" << endl;
	
	// ask user to enter valid filename
	cout << "Enter valid filename (no blanks!): ";
	cin >> filename;

	inputFile.open(filename.c_str());

	// if file not valid ask for new filename
	while(!inputFile)
	{
		inputFile.clear();
		cout << "Re-Enter a valid filename: ";
		cin >> filename;
		inputFile.open(filename.c_str());
	}

	cout << "========================================================" << endl;
	cout << "                   Text of the File" << endl;
	cout << "========================================================" << endl;

	// reading text of file
	inputFile.get (ch);
	
	while (inputFile)
	{
		charCount ++;
		cout << ch;
		
		if (ch == '\n')
		{
			lineCount ++;
		}
		
		if (ch == TAG_OPEN)
		{
			tagCount ++;
		}
		
		if ((ch == '<a') && (ch == '<A'))
		{
			link = true;
			linkCount ++;
		}

		if (ch == '<!--')
		{
			commentCount ++;
		}

		inputFile.get (ch);

	}
Please provide the compiler errors.

To start, string literals should be enclosed in double quotes and character literals are enclosed in single quotes. It appears that the a single character, ch, is being compared against a string. In the code above, ch will only contain a single character at a time.

For example:
1
2
    string s = "a string"; // could be empty (""), or contain 1 or more characters
    char  ch = '\n';       // exactly 1 character 


Also, on line 64, I suspect that you meant to use the logical or (||) not logical and (&&).
Last edited on
there are no compiling errors, it just reads the linkcount as being 0, and comment count as being 0
In that case, I'd bet the compiler is truncating '<a' down to '<' or something similar--which still seems odd.

You could identify a '<' and then read another character to see if it's an 'a' or 'A' and then increment the link count...
would i use a bool statement... i really have no idea what im doing at this point
come back i need your help!
Ok, here's what I would do:

Start by moving the get call on line 75 to the top of the loop (remove the one on line 47) and move the cout statement down to the bottom of the loop. That way you can test the character before you decide to output it.

Then, add a clause to check for '<' that will then proceed to check the next character if it matched. Here's a simple example:
1
2
3
4
5
6
7
8
9
10
11
12
13
if( '<' == ch )
{
    // we have a '<', so we need to see what's next

    // Note: output ch before we overwrite it with the next one and
    // remember to increment charCount
    cout << ch;
    inputFile.get( ch );
    if( 'a' == ch )
    {
        // we have "<a"        
    }
}

Last edited on
Topic archived. No new replies allowed.